繁体   English   中英

模型未在asp.net MVC中用Ajax发布更新?

[英]Model is not updated with ajax post in asp.net mvc?

我正在使用asp.net mvc应用程序。 在我的查看页面中,我有如下代码所示的文本框:

@Html.TextBoxFor(m => m.Id, new { @class = "form-control", @readonly = "readonly" })

另外,在表单中提交ajax帖子,

$('#btnSubmit').click(function(e)
    {
        $.ajax({
            url: '/button/Button',
            type: 'POST',
            data: {Id:1},
           });
    });

我的控制器:

 MasterM empcode = new MasterM();

        [HttpGet]
        public ActionResult Button()
        {

            var empcode = new MasterM();

            return View(empcode);
         }

        [HttpPost]
        public  ActionResult Button(MasterM model)
        {
            ModelState.Clear();
            if (!ModelState.IsValid)
            {
                return View(model);
            }
            return View(model);
        }

此表单提交后,文本框的值应更改为新值(1)。 我如何在控制器部分本身中实现这一目标。 我知道ajax成功后,我们可以手动更新元素值,但是我想在控制器部分实现这一点,并使用新值更新模型。

当您发布时,您只发送一个ID,而不发送整个模型,因此基本上

public  ActionResult Button(MasterM model) will be empty 

并且由于您要返回空集,因此不会更新该文本框。

您可以在此处执行以下两项操作:1)将接收类型修改为

 Public  ActionResult Button(int Id)
    {
    MasterM model = new MasterM();
    model.Id = Id;
     if (!ModelState.IsValid)
                {
                    return View(model);
                }
                return View(model);
    }

2)如果要将Model保留为Controller中的参数,则需要在Jquery中序列化数据并将其发布。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM