如何将模型属性绑定到Httppost数据 [英] How Do I Bind Model Property To Httppost Data

查看:149
本文介绍了如何将模型属性绑定到Httppost数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

美好的一天,我在我的模型类中设置了如图所示的属性

  public   class 类别
{
public string statename { get ; set ; }
}





i从我的视图中使用ajax得到一个变量,如下所示,

< pre lang =javascript>< script language = javascript>
function getlgass(){

var staname = $( ' #mylist option:selected')。text(); // 这是一个htmlhelper下拉列表。我从这个列表中获取了所选文本并将其保存在该变量中

$ .ajax({
url:' / Ministry / sendProp'
类型:' POST'
data: JSON .stringify(staname),
contentType:' application / json; charset = utf-8'
成功: function (数据){alert(data.success);},
错误: function (){alert( 错误)}
}); // 我使用ajax将其发布到我的控制器函数

$ .getJSON(' @ Url.Action(getLGA,Ministry)' function (费用){
var $ sel = $( #schLga);
$ sel.empty();
$ .each(成本,函数(索引,元素){
$( < option />)。text(element).appendTo($ sel);
});
});
}

alert( 更改内容);
< / script>





i需要将该变量传递给我的模型中的属性,以便我可以在函数getLGA执行服务器请求

我的控制器功能如图所示

 [HttpPost] 
public ActionResult sendProp()
{
// i我想我应该在这里将值附加到我的模型属性,但我不知道如何。
return Json( new {success = true });
}

[AllowCrossSiteJson]
public ActionResult getLGA()
{
尝试
{
类别c = 类别();

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< 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 (例外情况)
{
throw ex;
}
}





感谢您的帮助。

解决方案

' #mylist option:selected')。text(); // 这是一个htmlhelper下拉列表。我从这个列表中获取了所选文本并将其保存在该变量中


.ajax({
url:' / Ministry / sendProp'
类型:' POST'
数据: JSON .stringify(staname),
contentType:' application / json; charset = utf-8'
成功: function (data){alert(data.success);},
error: function (){alert( 错误)}
}); // 我使用ajax


.getJSON(' @ Url.Action(getLGA,Ministry)' function (费用){
var


good day, i have a property set in my model class as shown

public class Category
    {
        public string statename { get; set; }
    }



i got a variable from my view using ajax as shown below,

<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>



i need to pass that variable to the property in my model so that i can use it in the function getLGA to perform a server request
my controller function are as shown

[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;
            }
        }



Thanks for your assistance.

解决方案

('#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


这篇关于如何将模型属性绑定到Httppost数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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