没有更新EditorFor值asp.net MVC3局部视图结果 [英] asp.net MVC3 partial view result not updating EditorFor value

查看:116
本文介绍了没有更新EditorFor值asp.net MVC3局部视图结果的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个简单的电子邮件捕获形式的我的主页的一部分,但是当我返回编辑表单模型值未得到更新的局部视图:

I have a simple email capture form as part of my home page, however when I return the partial view the model value for the Editor for form is not getting updated:

示范

public class Contact 
    {
        [Key]
        public int Id { get; set; }
        [Required]
        public string Email { get; set; }
        [NotMapped]
        public string Message { get; set; }
    }

在浏览

@model TestApp.Models.Contact

@{
    ViewBag.Title = "Home Page";
}

<script src="@Url.Content("~/Scripts/jquery.validate.min.js")" type="text/javascript"></script>
<script src="@Url.Content("~/Scripts/jquery.validate.unobtrusive.min.js")" type="text/javascript"></script>
<script src="@Url.Content("~/Scripts/jquery.form.js")" type="text/javascript"></script>

<script type="text/javascript" >
    $(function () {
        // ajax form post 
        $("#EmailForm").ajaxForm(function (result) {
            $("#EmailForm").html(result); // post results 
        });
    });
</script>
    @using (@Html.BeginForm("SaveEmail", "Home", FormMethod.Post, new { id = "EmailForm" }))
    { 
        @Html.Partial("SaveEmail", Model);
    }

该部分

@model TestApp.Models.Contact

<span class="editor-label">
    @Html.LabelFor(m => m.Email) :
</span>
<span class="editor-field">
    @Html.EditorFor(m => m.Email)
</span>
<input type="submit" value="Submit" />
<div id="message"> 
    @Html.ValidationMessageFor(m => m.Email) 
    @Html.DisplayFor(m => m.Message) 
</div>  

<br/>

控制器

  [HttpPost]
        public PartialViewResult SaveEmail(Contact contact)
        {
            if (ModelState.IsValid)
            {
                //TODO: save e-mail to database

                contact.Message = "You e-mail has been added to our database - Thank y   ou";
                contact.Email = "";
                    return PartialView(contact);

            }

            return PartialView();
        }

这将返回模型误差如预期,当模型的状态是有效的返回消息,但在响应电子邮件文本框的值没有更新

This returns model errors as expected, returns the message when model state is valid, but the email text box value in the response is not updating

响应形式萤火虫时发布电子邮件a@b.com

The response form firebug when posting e-mail a@b.com

<span class="editor-label">
    <label for="Email">Email</label> :
</span>
<span class="editor-field">
    <input class="text-box single-line" id="Email" name="Email" type="text" value="a@b.com" />
</span>
<input type="submit" value="Submit" />
<div id="message"> 

    You e-mail has been added to our database - Thank you 
</div>  

<br/>

我试图创造一种新的模式,并通过在,我试过ModelState.Clear(),我可以一点点的jQuery解决问题。

I've tried creating a new model and passing that in, I've tried ModelState.Clear(), I can fix the problem with a little jquery

 $("#Email").val(''); // clear the email address 

但是,这似乎有点哈克,我想明白是怎么回事。

But this seem a little hacky and I would like to understand what is going on.

推荐答案

添加ModelState.Clear()您在电子邮件字段设置为空字符串后返回的PartialView应该做的伎俩之前。您也可以尝试ModelState.Remove(电子邮件)。

Adding a ModelState.Clear() after you have set the Email field to empty string and just before you return the PartialView should do the trick. You could also try ModelState.Remove("Email").

请参阅this博客文章了解为什么内置佣工行为这种方式了解更多信息。

See this blog post for more information on why the built-in helpers behave this way.

这篇关于没有更新EditorFor值asp.net MVC3局部视图结果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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