2016-06-28 43 views
3

我有一個模式彈出,但我想對它做模型狀態驗證。問題是如果有模型狀態錯誤,它會返回頁面。如何將數據返回到彈出窗口,保持彈出窗口打開。這是我到目前爲止:返回ActionResult提交後提交模式彈出

[HttpPost] 
public ActionResult SaveProject(AddstuffViewModel model) 
{ 
    if (!ModelState.IsValid) 
    { 
     return PartialView("_Addstuff", model); 
    } 
} 



@model Models.AddStuffViewModel 

@using (Ajax.BeginForm(
    "SaveProject", "Projects", 
    new AjaxOptions { 
     HttpMethod = "POST", 
    }, 
    new { 
     id = "FormName", 
     role = "form" } 
    )) 
{ 
    <div class="panel-heading projectModal-heading"> 
     <h4 class="panel-title">Add stuff </h4> 
    </div> 
    <div class="panel-body"> 
     <p class="group"> 
       <div class="form-group"> 
        @Html.LabelFor(m => m. Stuffs.Name, new { @class = "control-label" }) 
        @Html.TextBoxFor(m => m.Stuffs.Name, new { @class = "form-control", @id = "InputName", @placeholder = "Stuffs Name" }) 
        @Html.ValidationMessageFor(m => m.Stuffs.Name, "", new { @class = "text-danger" }) 
       </div> 
       <div class="form-group"> 
        @Html.LabelFor(m => m.Stuffs.Description, new { @class = "control-label" }) 
        @Html.TextBoxFor(m => m.Stuffs.Description, new { @class = "form-control", @id = "InputDescription", @placeholder = "Description" }) 
        @Html.ValidationMessageFor(m => m.Stuffs.Description, "", new { @class = "text-danger" }) 
       </div> 
       <div class="form-group"> 
        @Html.LabelFor(m => m.Stuffs.Url, new { @class = "control-label" }) 
        @Html.TextBoxFor(m => m.Stuffs.Url, new { @class = "form-control", @id = "InputUrl", @placeholder = "Url" }) 
        @Html.ValidationMessageFor(m => m.Stuffs.Url, "", new { @class = "text-danger" }) 
       </div> 

      </div> 
     </p> 
    </div> 
    <div class="panel-footer"> 
     <input type="submit" value="stuff" class="btn btn-default" /> 
    </div> 
} 

模式打開罰款和所有的數據正確填充。我只想在SaveProject方法返回時保持它打開。

+0

您是否試圖在表單中放置@ Html.ValidationSummary(true)? –

+0

它沒有工作,但謝謝。 –

回答

2

討厭回答我自己的問題,但我有一個id-10-T錯誤。

我包括jquery.validate.unobtrusive而不是jquery.unobtrusive-ajax最初。我包括兩個現在它工作正常。