我正在尝试提交局部视图,激活局部视图验证并加载主视图(主视图将不会一直相同)。基本上,我只想更新局部视图而不影响主视图视图。现在,当我提交局部视图时,它会加载局部视图操作。
部分视图:
@using (Ajax.BeginForm("LeaveReply", "Contactus", new AjaxOptions { InsertionMode = InsertionMode.Replace, UpdateTargetId = "respond"))
{
<h3>Leave a reply</h3>
<p class="textarea-block">
<label><span>Message (required)</span></label>
@Html.ValidationMessageFor(o=>o.Message)
@Html.TextAreaFor(o => o.Message, new { @rows="6", @cols="80", @name="message",@class="form-control" })
</p>
<p class="input-block clearfix">
<label class="required"><span>Your Name (required)</span></label>
@Html.ValidationMessageFor(o=>o.Name)
@Html.TextBoxFor(o=>o.Name, new { @class="form-control" })
</p>
<p class="input-block">
<label class="required"><span>Email (required)</span></label>
@Html.ValidationMessageFor(o=>o.Email)
@Html.TextBoxFor(o=>o.Email, new { @class="form-control" })
</p>
<p class="input-block last">
<label class="required"><span>Website</span></label>
@Html.ValidationMessageFor(o=>o.WebSite)
@Html.TextBoxFor(o=>o.WebSite, new { @class="form-control" })
</p>
<div class="clear"></div>
<p class="contact-button clearfix">
<input type="submit" id="submit-contact" value="Submit">
</p>
<div class="clearfix"><br /></div>
<div class="kp-success">@ViewBag.Message</div>
<div id="response"></div>
}
主要观点:
<div id="respond" class="kopa-border-2">
@{Html.RenderPartial("_MessagePartial", new Classified.ViewModels.MessageViewModel());}
</div>
联系我们控制器
[HttpPost]
public PartialViewResult LeaveReply(MessageViewModel model)
{
JsonResult result = new JsonResult();
if (ModelState.IsValid)
{
}
return PartialView("_MessagePartial", model);
}
型号:
public class MessageViewModel
{
[Key]
public int ID { get; set; }
public string MessageType { get; set; }
[Required]
public string Name { get; set; }
[EmailAddress]
[Required]
public string Email { get; set; }
public string WebSite { get; set; }
[Required]
public string Message { get; set; }
public DateTime? AddedDateTime { get; set; }
public int? UserID { get; set; }
}
*主视图绑定到不同的模型。