在 ASP.Net MVC3 中使用 jQuery 动态填充下拉列表 [英] Dynamically populate the drop-down using jQuery in ASP.Net MVC3
问题描述
我有两个模型:
公开课ProfessionalModels{公共字符串全名 { 获取;放;}公共 int ID { 获取;放;}}
和
公共类ClassModels{公共 int ID { 获取;放;}公共字符串教授{得到;放;}公共十进制名称 { 获取;放;}}
在我的视图中有一个添加类的表单:
@model MvcApp.Models.ClassModels@{ViewBag.Title = "创建";}<h2>创建</h2><script src="@Url.Content("~/Scripts/jquery.validate.min.js")" type="text/javascript"></script><script src="@Url.Content("~/Scripts/jquery.validate.unobtrusive.min.js")" type="text/javascript"></script>@using (Html.BeginForm()) {@Html.ValidationSummary(true)<字段集><legend>ClassModels</legend><div class="editor-label">@Html.LabelFor(model => model.Name)
<div class="editor-field">@Html.EditorFor(model => model.Name)@Html.ValidationMessageFor(model =>model.Name)
<p><输入类型=提交"值=创建"/></p></fieldset>}<div>@Html.ActionLink("返回列表", "索引")
我想在课堂视图中添加一个下拉菜单,其中列出了所有可用的教授.教授在 db 中,我可以轻松地从控制器调用 db 并将所有教授加载到某个列表/数组中.我需要有关如何使用 jQuery 向教授填充下拉列表的帮助.
在你的控制器中:
[HttpGet]公共虚拟 JsonResult LoadInfo(){var 查询 = _repository.GetInformation();//这里返回数据.返回 Json(查询,JsonRequestBehavior.AllowGet);}
那么在你看来:
然后使用 jQuery 加载下拉列表
function LoadInfo() {$.getJSON("@Url.Action(MVC.ControllerName.MethodName())", null,功能(数据){$("#info").empty();$.each(数据,函数(){$("#info").append($("<option/>").val(this.Id).text(this.Name));});});}
这假设 Id 和 Name 是您的对象的属性.您可以根据要加载的下拉列表使用 ID 和 FullName.我还使用 T4MVC 来获取不同的方法名称.
希望这会有所帮助,
I have two models:
public class ProfessorModels
{
public string FullName { get; set; }
public int ID { get; set; }
}
and
public class ClassModels
{
public int ID { get; set; }
public string Professor { get; set; }
public decimal Name { get; set; }
}
in my View there is a form to add the class:
@model MvcApp.Models.ClassModels
@{
ViewBag.Title = "Create";
}
<h2>Create</h2>
<script src="@Url.Content("~/Scripts/jquery.validate.min.js")" type="text/javascript"></script>
<script src="@Url.Content("~/Scripts/jquery.validate.unobtrusive.min.js")" type="text/javascript"></script>
@using (Html.BeginForm()) {
@Html.ValidationSummary(true)
<fieldset>
<legend>ClassModels</legend>
<div class="editor-label">
@Html.LabelFor(model => model.Name)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.Name)
@Html.ValidationMessageFor(model => model.Name)
</div>
<p>
<input type="submit" value="Create" />
</p>
</fieldset>
}
<div>
@Html.ActionLink("Back to List", "Index")
</div>
I would like to add a drop-down menu to the Class View, which lists all the available professors. Professors are in db, and I can easily make a call to db from controller and load all professors in to some list/array. I need help with how to populate the drop-down with professors using jQuery.
In your controller:
[HttpGet]
public virtual JsonResult LoadInfo()
{
var query = _repository.GetInformation(); //Here you return the data.
return Json(query, JsonRequestBehavior.AllowGet);
}
Then in your view:
<select id="info"></select>
Then you load the drop down using jQuery
function LoadInfo() {
$.getJSON("@Url.Action(MVC.ControllerName.MethodName())", null,
function (data) {
$("#info").empty();
$.each(data, function () {
$("#info").append($("<option />").val(this.Id).text(this.Name));
});
});
}
This assumes that Id and Name are properties of your object. You could use ID and FullName depending on which drop down you're loading. I also use T4MVC to get the different method names.
Hope this helps,
这篇关于在 ASP.Net MVC3 中使用 jQuery 动态填充下拉列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!