在单页中使用多个部分视图时出现重复条目

时间:2017-01-06 10:31:26

标签: asp.net-mvc asp.net-mvc-partialview

您好我在一个asp .net mvc页面中有两个部分视图。当我使用任何部分视图提交条目时,2个条目被保存在数据库中。如果我禁用任何一个局部视图页面工作正常。请帮忙。

我的部分观点如下:

@using (Ajax.BeginForm("Create", "EnquiryForm", new AjaxOptions { HttpMethod = "POST", UpdateTargetId = "divEnquiryFormMessage", LoadingElementId = "imgLoadingEnquiryForm" }))
{
    @Html.ValidationSummary(true, "", new { @class = "text-danger" })
    @Html.AntiForgeryToken()
    <div id="divEnquiryFormMessage">
    </div>
    <div class="form-group">
        @Html.EditorFor(model => model.Name, new { htmlAttributes = new { @class = "form-control", @placeholder = "Name" } })
        @Html.ValidationMessageFor(model => model.Name, "", new { @class = "col-sm-12 text-danger" })
    </div>
    <div class="form-group">
        @Html.EditorFor(model => model.ContactNumber, new { htmlAttributes = new { @class = "form-control", @placeholder = "Contact Number" } })
        @Html.ValidationMessageFor(model => model.ContactNumber, "", new { @class = "col-sm-12 text-danger" })
    </div>
    <div class="form-group">
        @Html.EditorFor(model => model.Email, new { htmlAttributes = new { @class = "form-control", @placeholder = "Email" } })
        @Html.ValidationMessageFor(model => model.Email, "", new { @class = "col-sm-12 text-danger" })
    </div>
    <div class="form-group">
        @Html.TextAreaFor(model => model.Comments, new { htmlAttributes = new { @class = "form-control", @placeholder = "Comments" } })
        @Html.ValidationMessageFor(model => model.Comments, "", new { @class = "col-sm-12 text-danger" })
    </div>

    <button type="submit" class="submit_btn">Submit</button>

<img style="display:none;" src="~/Content/Images/LoadingImage.gif" id="imgLoadingEnquiryForm" />

}
<script src="~/Scripts/jquery.unobtrusive-ajax.min.js"></script>

第二部分观点:

@using (Ajax.BeginForm("Create", "MailingList", new AjaxOptions { HttpMethod = "POST", UpdateTargetId = "divMessage", LoadingElementId = "imgLoading" }))
{
    @Html.ValidationSummary(true, "", new { @class = "text-danger" })
    @Html.AntiForgeryToken()
    <div id="divMessage">
    </div>
        <div class="form-group">
            @Html.EditorFor(model => model.FirstName, new { htmlAttributes = new { @class = "form-control", @placeholder = "First Name" } })
            @Html.ValidationMessageFor(model => model.FirstName, "", new { @class = "col-sm-12 text-danger" })
        </div>
        <div class="form-group">
            @Html.EditorFor(model => model.LastName, new { htmlAttributes = new { @class = "form-control", @placeholder = "Last Name" } })
            @Html.ValidationMessageFor(model => model.LastName, "", new { @class = "col-sm-12 text-danger" })
        </div>
        <div class="form-group">
            @Html.EditorFor(model => model.Email, new { htmlAttributes = new { @class = "form-control", @placeholder = "Email" } })
            @Html.ValidationMessageFor(model => model.Email, "", new { @class = "col-sm-12 text-danger" })
        </div>
        <div class="form-group">
            @Html.EditorFor(model => model.ZipCode, new { htmlAttributes = new { @class = "form-control", @placeholder = "Zip Code" } })
            @Html.ValidationMessageFor(model => model.ZipCode, "", new { @class = "col-sm-12 text-danger" })
        </div>
        <button type="submit" class="submit_btn">Submit</button><img style="display:none;" src="~/Content/Images/LoadingImage.gif" id="imgLoading" />

}
<script src="~/Scripts/jquery.unobtrusive-ajax.min.js"></script>

两个部分视图都在击中不同的控制器和方法仍然存在奇怪的情况。我多事了#34;提交&#34;按钮导致问题,但我不知道如何解决此问题。

其他问题:保存内容后我想在部分视图中清空输入控件但它也无效。我试过ModelState.Clear()

我的控制器:

 [HttpPost]
        [ValidateAntiForgeryToken]
        public string Create(EnquiryFormViewModel vObj)
        {
            System.Threading.Thread.Sleep(1000);
            if (ModelState.IsValid)
            {
                EnquiryForm mObj = new EnquiryForm();
                mObj.Name = vObj.Name;
                mObj.ContactNumber = vObj.ContactNumber;
                mObj.Email = vObj.Email;
                mObj.Comments = vObj.Comments;
                mObj.IsContacted = false;
                mObj.CreatedOn = DateTime.Now;
                string status = enquiryFormService.insertEnquiryForm(mObj);
                ModelState.Clear();
                return status;
            }
            else
            {
                return "<p class=\"bg-danger\">Oops there are some errors on page.</p>";
            }

        }

2 个答案:

答案 0 :(得分:0)

你的整个页面应该只有1个Ajax不引人注意,否则你的控制器会多次击中

答案 1 :(得分:0)

您可以在提交表单后使用成功方法,如下所示: -

@using (Ajax.BeginForm("Create", "MailingList", new AjaxOptions { onSuccess="ResetForm",HttpMethod = "POST", UpdateTargetId = "divMessage", LoadingElementId = "imgLoading" }))

Function ResetForm()标签内创建<script>并在Inside函数中写入  $('#formID')[0].reset();