2016-10-04 77 views
0

你好,我不能保留多個帖子的值,我在我的控制器中的「Post1」動作函數總是將MyViewModelObj.Field2設爲null。我期望它在第二個帖子中保留舊值 如何使MyViewModel模型類對象保持值?在多個帖子之間保留值

Mymodels.cs(型號)

namespace RetainTest.Models 
{ 

    public class MyViewModel 
    { 

     public string Field1 { get; set; } 
     public string Field2 { get; set; } 
     public string Field3 { get; set; } 
     public string Field4 { get; set; } 
    } 
} 

RetainView.cshtml(查看)

@model RetainTest.Models.MyViewModel 

@{ 
    ViewBag.Title = "RetainView"; 
} 

<h2>RetainView</h2> 


@using (Html.BeginForm("Post1", "Retain", FormMethod.Post, 
          new { enctype = "multipart/form-data" })) 
{ 
    @Html.AntiForgeryToken() 
    @Html.HiddenFor(model => model.Field1); 
    @Html.HiddenFor(model => model.Field2); 
    @Html.HiddenFor(model => model.Field3); 
    @Html.HiddenFor(model => model.Field4); 

    <div class="form-horizontal"> 
     <h4>MyViewModel</h4> 
     <hr /> 
     @Html.ValidationSummary(true, "", new { @class = "text-danger" }) 
     <div class="form-group"> 
      @Html.LabelFor(model => model.Field1, htmlAttributes: new { @class = "control-label col-md-2" }) 
      <div class="col-md-10"> 
       @Html.EditorFor(model => model.Field1, new { htmlAttributes = new { @class = "form-control" } }) 
       @Html.ValidationMessageFor(model => model.Field1, "", new { @class = "text-danger" }) 
      </div> 
     </div> 

     @{ 
      if ( Model.Field2 == "Val2") 
      { 

       <div class="form-group"> 
        @Html.LabelFor(model => model.Field2, htmlAttributes: new { @class = "control-label col-md-2" }) 
        <div class="col-md-10"> 
         @Html.EditorFor(model => model.Field2, new { htmlAttributes = new { @class = "form-control" } }) 
         @Html.ValidationMessageFor(model => model.Field2, "", new { @class = "text-danger" }) 
        </div> 
       </div> 

      } 
     } 
     <div class="form-group"> 
      <div class="col-md-offset-2 col-md-10"> 
       <input type="submit" value="Create" class="btn btn-default" /> 
      </div> 
     </div> 
    </div> 
} 

<div> 
    @Html.ActionLink("Back to List", "Index") 
</div> 

@section Scripts { 
    @Scripts.Render("~/bundles/jqueryval") 
} 

RetainController.cs(控制器)

namespace RetainTest.Models 
{ 
    public class RetainController : Controller 
    { 
     // GET: Retain 
     public ActionResult Index() 
     { 
      MyViewModel MyViewModelObj = new MyViewModel(); 

      MyViewModelObj.Field1 = "Val1"; 
      return View("RetainView", MyViewModelObj); 
     } 
     [HttpPost] 
     public ActionResult Post1(MyViewModel MyViewModelObj) 
     { 
      if (string.IsNullOrEmpty(MyViewModelObj.Field2)) 
      { 
       MyViewModelObj.Field2 = "Val2"; 
      } 
      return View("RetainView", MyViewModelObj); 
     } 

    } 
} 
+0

您的視圖對於您的模型的每個屬性都有一個隱藏的輸入。 'DefaultModelBinder'爲請求中的每個屬性讀取第一個名稱/值對,並忽略所有其他屬性。由於隱藏的輸入是第一個,所以'EditorFor()'方法的值將被忽略。你只能綁定模型的初始值,而不是編輯的值(使得你的表單毫無意義)。刪除隱藏的輸入! –

+0

和'MyViewModelObj.Field2 =「Val2」;'什麼都不做(參考[這個答案]的第二部分(http://stackoverflow.com/questions/26654862/textboxfor-displaying-initial-value-not-the-值更新的代碼/ 26664111#26664111)的解釋)。如果你想用不同的值顯示一個視圖,那麼按照PRG模式(或者使用'ModelState.Clear();') –

+0

謝謝@StephenMuecke,刪除這三個語句修復了它。 @ Html.HiddenFor(model => model.Field1); @ Html.HiddenFor(model => model.Field2); @ Html.HiddenFor(model => model.Field3); @ Html.HiddenFor(model => model.Field4); 我雖然隱藏的價值是一個職位,我意識到它永遠保留它 – AnnaDaKhokha

回答

0

得到答案

「你的觀點對模型的每個屬性的隱藏輸入該DefaultModelBinder讀取請求中的每個屬性名/值對,而忽略其他的。由於隱藏輸入是第一個,你的EditorFor()方法的值將被忽略,你只能綁定你的模型的初始值,而不是編輯的值(使得表單沒有意義),去掉隱藏的輸入!

0

試試這個:

namespace RetainTest.Models 
{ 

    public class MyViewModel 
    { 
     [Required(ErrorMessage = "Field1 is required")] 
     public string Field1 { get; set; } 

     [Required(ErrorMessage = "Field2 is required")] 
     public string Field2 { get; set; } 

     public string Field3 { get; set; } 

     public string Field4 { get; set; } 
    } 
} 

@model RetainTest.Models.MyViewModel 

@{ 
    ViewBag.Title = "RetainView"; 
} 

<h2>RetainView</h2> 
@using (Html.BeginForm("Post1", "Retain", FormMethod.Post, new { id = "form", enctype = "multipart/form-data"})) 
{ 
@Html.AntiForgeryToken() 
@Html.HiddenFor(model => model.Field1); 
@Html.HiddenFor(model => model.Field2); 
@Html.HiddenFor(model => model.Field3); 
@Html.HiddenFor(model => model.Field4); 

<div class="form-horizontal"> 
    <h4>MyViewModel</h4> 
    <hr /> 
    @Html.ValidationSummary(true, "", new { @class = "text-danger" }) 
    <div class="form-group"> 
     <Label for="Field1" class="control-label col-md-2">Field1 </label> 
     <div class="col-md-10"> 
      @if(Model.Field1 == "Val1") 
      { 
       <input type="text" name="Field1" id="Field1" class="form-control" value="@Model.Field1"> 
      } 
      else 
      { 
       <input type="text" name="Field1" id="Field1" class="form-control"> 
      } 
      <span class="field-validation-valid text-danger" 
        data-valmsg-for="Field1" 
        data-valmsg-replace="true"> 
      </span> 
     </div> 
    </div> 
    <div class="form-group"> 
     <Label for="Field2" class="control-label col-md-2">Field2 </label> 
     <div class="col-md-10"> 
      @if(Model.Field2 == "Val2") 
      { 
       <input type="text" name="Field2" id="Field2" class="form-control" value="@Model.Field2"> 
      } 
      else 
      { 
       <input type="text" name="Field2" id="Field2" class="form-control"> 
      } 
      <span class="field-validation-valid text-danger" 
       data-valmsg-for="Field2" 
       data-valmsg-replace="true"> 
      </span> 
     </div> 
    </div>    
    <div class="form-group"> 
     <div class="col-md-offset-2 col-md-10"> 
      <input type="submit" value="Create" class="btn btn-default" /> 
     </div> 
    </div> 
</div> 
} 

<div> 
    @Html.ActionLink("Back to List", "Index") 
</div> 

@section Scripts { 
    @Scripts.Render("~/bundles/jqueryval") 
} 


namespace RetainTest.Models 
{ 
    public class RetainController : Controller 
    { 
     private MyViewModel MyViewModelObj = new MyViewModel(); 

     // GET 
     public ActionResult Index() 
     {  
      if (Session["MyObj"] != null)    
       MyViewModelObj = (MyViewModel)Session["MyObj"];    
      else 
       MyViewModelObj.Field1 = "Val1";    

      return View("RetainView", this); 
     } 

     [AcceptVerbs(HttpVerbs.Post)] 
     [ValidateInput(false)] 
     public ActionResult Post1(FormCollection form) 
     { 
      MyViewModelObj.Field1 = form.Get("Field1"); 
      MyViewModelObj.Field2 = form.Get("Field2"); 

      if (string.IsNullOrEmpty(MyViewModelObj.Field2))     
       MyViewModelObj.Field2 = "Val2";    

      // here you need to store the data somewhere! 
      // session, database. 
      // just an example: 
      Session.Add("MyObj", MyViewModelObj); 

      return View("RetainView", this); 
     } 
    } 
} 

我使用會話來保留對象的值,但還有其他一些方法來存儲數據。上面的代碼會將用戶輸入發佈到控制器,並保留會話中的值。從斯蒂芬·馬克

+0

謝謝@ V.T。我沒有嘗試,但刪除隱藏的值固定它 – AnnaDaKhokha