ASP.NET MVC 3 Ajax.BeginForm和Html.TextBoxFor不能反映服务器上完成修改 [英] ASP.NET MVC 3 Ajax.BeginForm and Html.TextBoxFor does not reflect changes done on the server

查看:92
本文介绍了ASP.NET MVC 3 Ajax.BeginForm和Html.TextBoxFor不能反映服务器上完成修改的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用的是Ajax.BeginForm帮手ASP.NET MVC3提交替换本身的形式在服务器上设置新的价值形式。然而,当我使用佣工像Html.TextBoxFor我得到的不是我插入到服务器上的型号。值已提交的值,

例如,我设置someValue中4个在我的动作,并显示在一个文本框。我将值改为8,点击提交,并希望要更改的值回4在文本框中,但由于某种原因它停留8.但是,如果我输出someValue中不使用HTML辅助它说,4人有一些关于线索到底是怎么回事?

我的控制器:

 公众的ActionResult指数(HomeModel模型)
{
    model.SomeValue = 4;
    如果(Request.IsAjaxRequest())
        返回PartialView(模型);
    返回查看(模型);
}
公共类HomeModel
{
    公众诠释? someValue中{获得;组; }
}
 

我的视图(请注意,我在我的布局页面中的所有需要​​的JavaScript):

 < D​​IV ID =ajaxtest>
@using(Ajax.BeginForm(新AjaxOptions {InsertionMode = InsertionMode.Replace,
    UpdateTargetId =ajaxtest,HttpMethod =邮报})){
    @ Html.TextBoxFor(型号=> model.SomeValue)
    <输入类型=提交值=更新/>
}
< / DIV>
 

解决方案

您可以使用

  ModelState.Clear()
 

在你的控制器的方法,使HTML辅助使用改变模式。否则,他们使用的值从表单提交

看一看: Asp.net MVC ModelState.Clear

I'm using the Ajax.BeginForm helper in ASP.NET MVC3 to submit a form that replaces itself with new values in the form set on the server. However when I use helpers like Html.TextBoxFor I get the values that was submitted, not the values I inserted into the model on the server.

For example; I set SomeValue to 4 in my action and show it in a textbox. I change the value to 8, hit submit and would expect the value to be changed back to 4 in the textbox, but for some reason it stays 8. But if I output SomeValue without using Html helpers it says 4. Anybody have some clue about what is going on?

My controller:

public ActionResult Index(HomeModel model)
{
    model.SomeValue = 4;
    if (Request.IsAjaxRequest())
        return PartialView(model);
    return View(model);
}
public class HomeModel
{
    public int? SomeValue { get; set; }
}

My View (please not that I have all the needed javascript in my layout page):

<div id="ajaxtest">
@using(Ajax.BeginForm(new AjaxOptions{ InsertionMode = InsertionMode.Replace,
    UpdateTargetId = "ajaxtest", HttpMethod = "Post" })) {
    @Html.TextBoxFor(model => model.SomeValue)
    <input type="submit" value="Update" />
}
</div>

解决方案

you can use

ModelState.Clear() 

in your controller method to make the html helpers use your changed model. Otherwise they use the values from the form submit

Have a look at: Asp.net MVC ModelState.Clear

这篇关于ASP.NET MVC 3 Ajax.BeginForm和Html.TextBoxFor不能反映服务器上完成修改的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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