本文介绍了Mvc,HTML控件,jquery绑定未在formcollection中显示的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 29岁程序员,3月因学历无情被辞! 大家好, 我有位置部门和指定DropDown在视野和一些隐藏的领域,下拉通过Jquery绑定。 来自Ajax的返回值操作方法是实体框架JSON,它很简单一个var Type ToList。 现在,当我提交我的表格时,它只显示第一个下拉列表@Html.DropDownList(位置,< - 选择位置 - >) 请回复。 < div class =行> @using(Html.BeginForm()) { < div class =input-group col-md -6 col-md-offset-3> @ Html.DropDownList(位置,< - 选择位置 - >) < input type =hiddenid =hfLocationvalue =0 /> < br /> < br /> < select id =Department> < / select> < input type =hiddenid =hfDepartment/> < br /> < br /> < select id =Designation> < / select> ; < input type =hiddenid =hfDesignation/> < br /> < br /> < input type =submitvalue =显示员工/> < script type =text / javascript> $(function(){ $('select') .each(function(){ if($(this).find(option)。length< = 1){ $(this).attr (禁用,禁用); } }) }); $(select)。change(function(){ var value = 0; if($(this).val() !=){ value = $(this).val(); } VAR id = $(this).attr(id); $ .ajax({ 类型:发布, url:/ Home / AjaxMethod, 数据:'{DropdownValue:'+ value +',DropdownID:'+ id +'}', contentType:application / json; charset = utf-8, dataType:json, 成功:功能(响应){ var dropDownId; var list; switch(id){ caseLocation: list = response; $('#hfLocation')。val(value); DisableDropDown(#Department,< - 选择部门 - > ;); DisableDropDown(#Designation,< - Select Designation - >); PopulateDropDown(#Department,list) ; 休息; 案例部门: list = respon se; $('#hfDepartment')。val(value); DisableDropDown(#Designation,< - Select Designation - > ); PopulateDropDown(#Designation,list); break; caseDesignation: list = response; $('#hfDesignation')。val(value); } }, 失败:功能(响应){ 警报('失败') alert(response.responseText); }, 错误:函数(响应){ if(response.responseText!=' ') alert(response.responseText); } }); }); 函数DisableDropDown(dropDownId,Text){ $(dropDownId).attr(disabled,已禁用); $(dropDownId).empty()。append('< option selected =selectedvalue =0>'+ Text +'< / option>' ); } 函数PopulateDropDown(dropDownId,list){ if(list!= null& ;&安培; list.length> 0){ $(dropDownId).removeAttr(禁用); $ .each(list,function(){ $ (dropDownId).append($(< option>< / option>)。val(this ['ID'])。html(this ['Text'])); }); } } < / script> < / div> } < / div>Hi All,I Have Location Department And Designation DropDown In View And Some Hidden Field, And Dropdown Was Bind Through Jquery.Return Value From Ajax Action Method is Entity Framework JSON and It Was Simple A var Type ToList.Now When I Submit My Form It Showing Only First Dropdown Which Was @Html.DropDownList("Location", "<-- Select Location -->")Please Reply.<div class="row"> @using (Html.BeginForm()) { <div class="input-group col-md-6 col-md-offset-3"> @Html.DropDownList("Location", "<-- Select Location -->") <input type="hidden" id="hfLocation" value="0" /> <br /> <br /> <select id="Department"> </select> <input type="hidden" id="hfDepartment" /> <br /> <br /> <select id="Designation"> </select> <input type="hidden" id="hfDesignation" /> <br /> <br /> <input type="submit" value="Show Employee" /> <script type="text/javascript"> $(function () { $('select').each(function () { if ($(this).find("option").length <= 1) { $(this).attr("disabled", "disabled"); } }) }); $("select").change(function () { var value = 0; if ($(this).val() != "") { value = $(this).val(); } var id = $(this).attr("id"); $.ajax({ type: "Post", url: "/Home/AjaxMethod", data: '{DropdownValue : ' + value + ',DropdownID : "' + id + '"}', contentType: "application/json; charset=utf-8", dataType: "json", success: function (response) { var dropDownId; var list; switch (id) { case "Location": list = response; $('#hfLocation').val(value); DisableDropDown("#Department", "<-- Select Department -->"); DisableDropDown("#Designation", "<-- Select Designation -->"); PopulateDropDown("#Department", list); break; case "Department": list = response; $('#hfDepartment').val(value); DisableDropDown("#Designation", "<-- Select Designation -->"); PopulateDropDown("#Designation", list); break; case "Designation": list = response; $('#hfDesignation').val(value); } }, failure: function (response) { alert('Failure') alert(response.responseText); }, error: function (response) { if (response.responseText != '') alert(response.responseText); } }); }); function DisableDropDown(dropDownId, Text) { $(dropDownId).attr("disabled", "disabled"); $(dropDownId).empty().append('<option selected="selected" value="0">' + Text + '</option>'); } function PopulateDropDown(dropDownId, list) { if (list != null && list.length > 0) { $(dropDownId).removeAttr("disabled"); $.each(list, function () { $(dropDownId).append($("<option></option>").val(this['ID']).html(this['Text'])); }); } } </script> </div> }</div>public class HomeController : Controller { CallLoggerEntities callLoggerEntities = new CallLoggerEntities(); [HttpGet] public ActionResult Index() { var LocationEntity = new SelectList((from L in callLoggerEntities.Locations where (L.IsDel == false || L.IsDel == null) select new { ID = L.ID, Location = L.Location1 }), "ID", "Location").ToList(); ViewBag.Location = LocationEntity; return View(); } [HttpPost] [ActionName("Index") ] public ActionResult Index_Post(FormCollection F) { string L = F["Location"]; return View(); } public JsonResult AjaxMethod(string DropdownValue, string DropdownID) { int ID = Convert.ToInt32(DropdownValue); if (DropdownID == "Location") { var DepEntity = (from D in callLoggerEntities.Departments where ((D.IsDel == false || D.IsDel == null) && D.LocationID == ID) select new { ID = D.ID, Text = D.DepartmentName }).ToList(); return Json(DepEntity, JsonRequestBehavior.AllowGet); } if (DropdownID == "Department") { var DesEntity = (from D in callLoggerEntities.Designations where ((D.IsDel == false || D.IsDel == null) && D.DepartmentID == ID) select new { ID = D.ID, Text = D.DesignationName }).ToList(); return Json(DesEntity, JsonRequestBehavior.AllowGet); } if (DropdownID == "Designation") { List<FetchEmployeeList_Result> Result = callLoggerEntities.FetchEmployeeList("A", "A", "A", "A").ToList(); } return null; } public ActionResult About() { ViewBag.Message = "Your app description page."; return View(); } public ActionResult Contact() { ViewBag.Message = "Your contact page."; return View(); } } 我尝试了什么: MVC,HTML控件,Jquery绑定不在FormCollection中显示不工作What I have tried:MVC, HTML Control, Jquery Bind Not Showing In FormCollection Not Working推荐答案(function(){(function () {('select')。each(function(){ if(('select').each(function () { if ((this ).find(option)。length< = 1){(this).find("option").length <= 1) { 这篇关于Mvc,HTML控件,jquery绑定未在formcollection中显示的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云!
08-26 05:24
查看更多