asp.net MVC3 部分视图结果不更新 EditorFor 值

发布于 2024-11-15 11:05:45 字数 2922 浏览 1 评论 0原文

我有一个简单的电子邮件捕获表单作为主页的一部分,但是当我返回部分视图时,表单编辑器的模型值没有更新:

模型

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();
        }

这会按预期返回模型错误,返回模型状态有效时的消息,但响应中的电子邮件文本框值未更新

发布电子邮件时的响应表单 firebug [email protected]

<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="[email protected]" />
</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 解决问题

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

但是这看起来有点老套,我想了解发生了什么。

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:

The Model

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

The Views

@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);
    }

The Partial

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

The Controller

  [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

The response form firebug when posting e-mail [email protected]

<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="[email protected]" />
</span>
<input type="submit" value="Submit" />
<div id="message"> 

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

<br/>

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

套路撩心 2024-11-22 11:05:46

在将电子邮件字段设置为空字符串之后、返回 PartialView 之前添加 ModelState.Clear() 应该可以解决问题。您也可以尝试 ModelState.Remove("Email")。

请参阅 这篇博文了解有关内置帮助程序为何如此行为的更多信息。

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").

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文