Javascript 如何将视图数据绑定到模型属性

Javascript 如何将视图数据绑定到模型属性,javascript,c#,jquery,ajax,asp.net-mvc-5,Javascript,C#,Jquery,Ajax,Asp.net Mvc 5,我有一个这样的模型 public class Category { public string statename { get; set; } } 我使用ajax从视图中得到一个变量,如下所示 <script language="javascript"> function getlgass() {   var staname = $('#mylist option:selected').text();//this is a htmlhelper dropdownlist. i go

我有一个这样的模型

public class Category { public string statename { get; set; } }
我使用ajax从视图中得到一个变量,如下所示

<script language="javascript"> function getlgass() {   var staname = $('#mylist option:selected').text();//this is a htmlhelper dropdownlist. i got the selected text from this list and saved it in that variable $.ajax({ url:'/Ministry/sendProp', type:'POST', data: JSON.stringify(staname), contentType: 'application/json; charset=utf-8', success: function (data) { alert(data.success); }, error: function(){alert("Error")} });// i'm posting it to my controller function using ajax $.getJSON('@Url.Action("getLGA","Ministry")', function (costs) { var $sel = $("#schLga"); $sel.empty(); $.each(costs, function (index, element) { $("<option/>").text(element).appendTo($sel); }); }); }   alert("something changed"); </script>
function getlgass(){var staname=$('#mylist option:selected').text();//这是一个htmlhelper下拉列表。我从该列表中获取了所选文本,并将其保存在变量$.ajax({url:'/Ministry/sendProp',type:'POST',data:JSON.stringify(staname),contentType:'application/JSON;charset=utf-8',success:function(data){alert){(data.success);},error:function(){alert(“error”)}};//我正在使用ajax$.getJSON('@Url.Action(“getLGA”、“Ministry”),(function)(costs){var$sel=$(“#schLga”);$sel.empty()$。每个(costs,function(index,element){$(“”)。text(element)。appendTo($sel);});)警报(“发生了变化的事情”);
我需要将该变量传递给模型中的属性,以便在函数getLGA中使用它来执行服务器请求 我的控制器功能如图所示

  [HttpPost] public ActionResult sendProp() { // i think i'm supposed to attach the value to my model property here, but i don't know how. return Json(new { success = true }); }   [AllowCrossSiteJson] public ActionResult getLGA() { try { Category c = new Category();   getMethods.getState(c); char[] chars = { ',' };   int statepos = c.Name.IndexOf(c.statename);//i need that value here string stateindex = c.ID.ElementAt(statepos).ToString();   testStealthServ.countries csd = new testStealthServ.countries(); List<string> sth = csd.getlocal(stateindex).ToList<string>(); foreach (var v in sth) { string[] splits = v.Split(chars);   c.lgaName.Add(splits.ElementAt(0)); c.lgaID.Add(splits.ElementAt(1)); }   return Json(c.lgaName, JsonRequestBehavior.AllowGet); } catch (Exception ex) { throw ex; } }
[HttpPost]public ActionResult sendProp(){//我想我应该在这里把这个值附加到我的模型属性上,但我不知道怎么做。返回Json(new{success=true};}[AllowCrossSiteJson]public ActionResult getLGA(){try{Category c=new Category();getMethods.getState(c);char[]chars={',};int statepos=c.Name.IndexOf(c.statename);//我在这里需要这个值string stateindex=c.ID.ElementAt(statepos.ToString();testStealthServ.countries csd=new testStealthServ.countries();List sth=csd.getlocal(stateindex.ToList();foreach(var v in sth){string[]splits=v.Split(chars);c.lgaName.Add(splits.ElementAt(0));c.lgaID.Add(splits.ElementAt(1));}返回Json(c.lgaName,JsonRequestBehavior.AllowGet);}捕获(异常ex){throw ex;}

感谢您的帮助

此处有一些术语未按预期使用,但您可以从这里开始使用

控制器:

[HttpPost]
public ActionResult sendProp(Category pModel) {
    //If you put a breakpoint here, pModel.statename will have the value from getlgass().
    string state = pModel.statename;
    return this.Json("success");
}
Javascript:

$.post('/ministry/sendProp', { statename: getlgass() }, function (data) { alert(data); });

请告诉我,我不明白你所说的填充是什么意思,你是说我应该做这个pModel.statename=//什么吗?这是我遗漏的一点。不,我的意思是,由于post数据中使用了statename:,MVC系统将在控制器开始处理之前为你填充该值。我无法从问题中判断你是否是感兴趣的是发布数据或从服务器检索数据以显示。我感兴趣的是从视图上的下拉框中检索数据并将其用于控制器函数。因此我认为,如果我可以在模型中创建一个属性来保存值,那么我可以从控制器函数getLGA()中检索值我更新了我的答案。希望它现在会更有帮助。这其中有一些神奇的地方。你可能需要尝试一下这个示例代码,并在调试器中浏览它。