我正在尝试使用ajax和控制器操作来填充下拉列表,但是当我单击触发操作的按钮时似乎没有发生任何事情:
的Ajax:
$.ajax({
url: '@Url.Action("GetBranch", "Report")',
type: "GET",
success: function (result) {
$("#dropdown2").append('<option value=' + result.BranchId + '>' + result.BranchName + '</option>');
}
});
控制器操作:
public ActionResult GetBranch()
{
var branch = new Branch();
var branchId = branch.BranchId;
var branchName = branch.BranchName;
return Json(new[]
{
new {Id = branchId, Value = branchName}
}, JsonRequestBehavior.AllowGet);
}
这是一种我不太熟悉的做法,因此可能会有一些明显缺失的东西。这里没有任何答案可以帮助我,因为我试图绑定到现有的<select>
;不是@DropdownListFor
似乎控制器操作没有被ajax调用命中,因此没有返回任何内容。
感谢您的帮助!
答案 0 :(得分:0)
public JsonResult GetBranch()
{
var branch = new Branch();
var branchs = new List<Branch>();
var branchId = "Branch01"; //If only your branch id datatype is string
branch.branchId = branchId;
var branchName = "Name of Branch";//Name you want to give for your branch
branch.branchName = branchName;
branchs.Add(branch);
return Json(branchs.Select(t => new {Text = t.branchName, Value = t.branchId}), JsonRequestBehavior.AllowGet);
}