MVC Bootstrap部分视图ValidationMessageFor在加载页面

时间:2017-05-19 20:01:02

标签: javascript jquery asp.net-mvc twitter-bootstrap partial-views

我正在做MVCBootstrap App

我打开Bootstrap Partial View from Jquery作为参数传递Model

Controller我返回Partial View,然后从Jquery打开它。

我发现的问题是ValidationMessageFor被执行en Partial View已加载...我无法找到原因。

这个过程是这样的...我有一个叫做JavaScript函数的Image ......

  

img id =" btnEmail" SRC ="〜/内容/图片/ Email.png"的onclick =" btnEmail('参数1',参数2)"

这是我的Jquery



  function btnEmail(nick, user_id) {
       --validate if user is logged and call function that call Partial View
        if (SearchLogin())
            SendMail();
    }

 function SendMail() {

            user_id = $('#CommentUser_id').val();
            nick = $('#LblCommentName').val();
            if (user_id == $('#User_id').val())
                return;

            var model = { Object_id: $("#Upload_id").val(), Nick: nick, UserDestination_id: $("#User_id").val(), Parent_id: null, UserOrigin_id: user_id};
            $.ajax(
                      {
                          type: "POST",
                          url: '@Url.Action("Email", "Contact")',
                          data: model,
                          success: function (result) {
                              $("#myModalContact").html(result).modal({ backdrop: "static" });
                          },
                         
                      });
            "static" });
        }




这是我的

Controller Method



 public async Task<ActionResult> Email(ContactModel model)
        {
            return PartialView("_Contact", model);
        }
&#13;
&#13;
&#13;

这是我的部分视图

如果我像这样打电话给我的部分视图

&#13;
&#13;
<div class="modal fade" id="myModalContact" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
        @Html.Partial("~/Views/Contact/_Contact.cshtml", new TableAvivaVoz.Models.ContactModel() { Object_id = Model.upload.Upload_id, Nick = Model.upload.Nick, UserDestination_id = Model.upload.User_id, Parent_id = null,UserOrigin_id=Model.User_id });
    </div>
&#13;
&#13;
&#13;

工作得很好......

这是My Partial View的一部分

&#13;
&#13;
@model  TableAvivaVoz.Models.ContactModel
@{
    AjaxOptions ajaxOpts = new AjaxOptions
    {
        InsertionMode = InsertionMode.Replace,
        HttpMethod = "POST",
        OnSuccess = "sucessContact",
    };
}
@using (Ajax.BeginForm("XXX", "Contact", ajaxOpts))
{
    <div class="modal-dialog">
        <div class="modal-content">
            <div class="modal-header">
                <h4 class="modal-title" id="myModalLabel">
                   
                </h4>
            </div>
            <div class="myPartialContact">
                <div class="alert alert-success hidden">
                </div>
            </div>
            @Html.AntiForgeryToken()
           
            <div style="padding-bottom:220px">
                <div class="modal-body">
                    <div class="col-md-10">
                        @Html.TextAreaFor(model => model.Description, 10, 80, new { @class = "txtDescQ", placeholder = "Ingresa un mensaje", @rows = 8, onclick = "OcultarRecomend();" })
                        @Html.ValidationMessageFor(model => model.Description, "", new { @class = "text-danger" })
                    </div>
                </div>
            </div>
                <div class="myPartialContactBtn modal-footer">
                    <button type="button" class="btn btn-default visible btnOk" data-dismiss="modal">Close</button>
                    <button type="submit" class="btn btn-primary visible btnOk" id="btnSave">Enviar</button>

                </div>

            </div><!-- /.modal-content -->
    </div><!-- /.modal -->
}
&#13;
&#13;
&#13;

任何想法有什么不对?

1 个答案:

答案 0 :(得分:1)

您在加载时遇到错误,因为您将错误的模型传递给操作Email(ContactModel model)。 当您使用ajax post时,Model binder会验证模型并将错误添加到模型状态。

要解决此问题,请执行以下操作:

public async Task<ActionResult> Email(ContactModel model)
{
     ModelState.Clear();    
     return PartialView("_Contact", model);
}

如果有帮助,请告诉我。