如何在mvc中使用linq选择状态和自动填充的城市 [英] how to select state and automatically populated city using linq in mvc

查看:103
本文介绍了如何在mvc中使用linq选择状态和自动填充的城市的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,

在这里,我有使用模型的州和城市的下拉列表。但现在我想根据州选择填充城市。任何人都可以帮助我。在这里使用linq所以请帮助我在这里使用数据库表



描述: -

对于州Stateid - >主键,州名



对于城市Cityid->主键,City-name,StateId->外键



任何人都可以帮助我使用 LINQ

解决方案

现在你可以通过进行ajax调用来实现。现在你的状态下拉列表获取所有项目(状态名称),因此当用户选择状态时,你可以获得该字段的值{如果你使用razor并获取值,则为@ Html.DropdownListFor()}提供一个id。在选择状态时,在java脚本中进行ajax调用,将状态值作为参数传递,并编写linq以获取状态的所有城市。

这里stateId需要是cities表中的外键。所以你的查询可能是这样的: - var cities = context.Cities.Where(c => c.StatesId == stateId)[stateId是在ajax调用中传递的参数]



希望你理解。

谢谢

:)


hi to all,
Here i have dropdown for state and city using model. but now i want populate city as per state selection.Can anybody help me.Here im using linq so kindly help me Here im using database table

Description:-
For state Stateid ->Primarykey,statename

For city Cityid->primary key, City-name , StateId->foreign key

Can anybody help me how to populate city as per state name using LINQ

解决方案

Now you can to that, by making an ajax call. Now your states dropdown gets all the items(state names), so when the user selects the state you can get the value of that field{provide an id to @Html.DropdownListFor()}if you are using razor and get the value. On select of the state make an ajax call in your java script passing the state value as parameter and write the linq to get all the cities for the states.
Here the stateId needs to be a foreign key in the cities table. So that your query may go like this:- var cities = context.Cities.Where(c=>c.StatesId==stateId)[stateId is the parameter that is passed in the ajax call]

hope you uderstood.
Thank you
:)


这篇关于如何在mvc中使用linq选择状态和自动填充的城市的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆