0

我有以下用于创建新网络信息的操作方法:-

public ActionResult CreateVMNetwork(int vmid)
        {
VMAssignIps vmips = new VMAssignIps()
{
TechnologyIP = new TechnologyIP() { TechnologyID = vmid},
IsTMSIPUnique = true,
IsTMSMACUnique = true
};
            return PartialView("_CreateVMNetwork",vmips);
}


 [HttpPost]
    public ActionResult CreateVMNetwork(VMAssignIps vmip)
    {
        if (ModelState.IsValid)
        {
            try
            {
                repository.InsertOrUpdateVMIPs(vmip.TechnologyIP,User.Identity.Name);
                repository.Save();
            return PartialView("_networkrow",vmip);

        }
        catch (Exception ex)
        {

            ModelState.AddModelError(string.Empty, "Error occurred: " + ex.InnerException.Message);
        }

    }
    return PartialView("_CreateVMNetwork", vmip);

}

我有以下 _CreateVMNetwork 视图:-

@model TMS.ViewModels.VMAssignIps
@using (Ajax.BeginForm("CreateVMNetwork", "VirtualMachine", new AjaxOptions

{
    InsertionMode = InsertionMode.InsertAfter,
    UpdateTargetId = "networktable",
    LoadingElementId = "loadingimag",
    HttpMethod= "POST"
}))
{
       @Html.ValidationSummary(true)
       @Html.HiddenFor(model=>model.TechnologyIP.TechnologyID)
    @Html.Partial("_CreateOrEditVMNetwork", Model)

       <input type="submit" value="Save" class="btn btn-primary"/>
}

和 _CreateOrEditVMNetwork 视图:-

@model TMS.ViewModels.VMAssignIps

<div>
<span class="f">IP Address</span> 

@Html.EditorFor(model => model.TechnologyIP.IPAddress)
@Html.ValidationMessageFor(model => model.TechnologyIP.IPAddress)                                              

   <input type="CheckBox" name="IsTMSIPUnique" value="true" @(Html.Raw(Model.IsTMSMACUnique ? "checked=\"checked\"" : "")) /> | 
 <span class="f"> MAC Address</span>       
@Html.EditorFor(model => model.TechnologyIP.MACAddress)
@Html.ValidationMessageFor(model => model.TechnologyIP.MACAddress)                                              

 <input type="CheckBox" name="IsTMSMACUnique" value="true" @(Html.Raw(Model.IsTMSMACUnique ? "checked=\"checked\"" : "")) />

</div>

我面临的问题是,如果添加新实体时出现模型状态错误,则会显示部分视图,模型状态错误如下:- 在此处输入图像描述

所以我的问题是,如果有办法用局部视图显示模型状态错误,而不像我目前所做的那样更新表格行“插入之后”?谢谢

4

1 回答 1

1

鉴于我猜你已经找到了解决这个问题的年龄,

但这是一个使用 InsertionMode.Replace 的示例,也许它可以帮助其他人。

从视图中截取

@using (Ajax.BeginForm("AddPerson", "Home", new AjaxOptions { HttpMethod = "POST", InsertionMode = InsertionMode.Replace, UpdateTargetId = "UpdateSection" }))
{

    <div id="UpdateSection">

        @Html.Partial("PersonModel", Model.Person)

    </div>

    <input type="submit" value="add" />
}

从控制器上截取

  if (!ModelState.IsValid)
    {

        return PartialView("AddPerson", Person);
    }

只需确保包含“jquery.unobtrusive-ajax.min.js”脚本(我不确定默认情况下)

于 2014-04-22T16:26:47.087 回答