2017-04-11 37 views
1

因此,本周我一直在研究一个简单的asp.net应用程序。今天我遇到了一个剃须刀视图问题,不会返回任何东西给我的控制器方法。该视图必须返回列表<>中的多个对象。使用for循环时,asp.net mvc视图仅返回

我终于通过将一个对象列表传递给视图并通过'for'循环遍历它们来工作。我今天也尝试过使用foreach循环,但没有奏效。

我已经得到了通过空对象的列表视图以下GET方法:

// GET: MpSwitches/CreateSeveral 
    public ActionResult CreateSeveral() 
    { 
     var mpSwitches = new List<MpSwitch> 
     { 
      new MpSwitch() {IpAdress = "1"}, 
      new MpSwitch() {IpAdress = "2"}, 
      new MpSwitch() {IpAdress = "3"}, 
      new MpSwitch() {IpAdress = "4"}, 
      new MpSwitch() {IpAdress = "5"} 
     }; 
     // Check if the user gets redirected to this method because of a First Time Startup. 
     if (TempData["RedirectFirstTimeStartup"] != null) 
     { 
      ViewBag.FirstTime = 
       "Je bent doorgestuurd naar deze pagina omdat er nog geen MP-switches in de database staan."; 
      return View(mpSwitches); 
     } 
     return View(mpSwitches); 
    } 

再有就是查看:

@model List<WebInterface.Models.MpSwitch> 
@{ 
    ViewBag.Title = "Create several"; 
} 
<h2>Create several</h2> 

@using (Html.BeginForm()) 
{ 
    @Html.AntiForgeryToken() 

    <div class="form-horizontal"> 
     <h4>MpSwitch</h4> 
     @if (ViewBag.FirstTime != null) 
     { 
      <div class="alert-warning"> 
       <b>Opgelet! </b>@ViewBag.FirstTime 
      </div> 
     } 
     @for (int i = 0; i < Model.Count; i++) 
     { 
      <hr /> 
      @Html.ValidationSummary(true, "", new { @class = "text-danger" }) 
      <div class="form-group"> 
       @Html.LabelFor(model => model[i].IpAdress, htmlAttributes: new { @class = "control-label col-md-2" }) 
       <div class="col-md-10"> 
        @Html.EditorFor(model => model[i].IpAdress, new { htmlAttributes = new { @class = "form-control" } }) 
        @Html.ValidationMessageFor(model => model[i].IpAdress, "", 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> 

我的问题是:为什么没有按”同样的代码使用'foreach'代替'for'循环吗?

当前未使用的代码:

@foreach (var item in Model) 
    { 
     <hr /> 
      @Html.ValidationSummary(true, "", new { @class = "text-danger"}) 
      <div class="form-group"> 
      @Html.LabelFor(modelItem => item.IpAdress, htmlAttributes: new { 
    @class = "control-label col-md-2" }) 
      <div class="col-md-10"> 
       @Html.EditorFor(modelItem => item.IpAdress, new { 
    htmlAttributes = new { @class = "form-control" } }) 
       @Html.ValidationMessageFor(modelItem => item.IpAdress, "", new 
    { @class = "text-danger" }) 
      </div> 
     </div> 
    } 

现在使用foreach循环以下控制器方法没有得到一个参数时(参数为空)。

// POST: MpSwitches/CreateSeveral 
    // To protect from overposting attacks, please enable the specific 
    properties you want to bind to, for 
    // more details see https://go.microsoft.com/fwlink/?LinkId=317598. 
    [HttpPost] 
    [ValidateAntiForgeryToken] 
    public ActionResult CreateSeveral([Bind(Include = 
    "Id,IpAdress,LastSyncDateTime")] List<MpSwitch> mpSwitches) 
    { 
     if (ModelState.IsValid) 
     { 
      foreach (var mpSwitch in mpSwitches) 
      { 
       db.MpSwitches.Add(mpSwitch); 
      } 
      db.SaveChanges(); 
      return RedirectToAction("Index"); 
     } 
     return View(mpSwitches); 
    } 

,我得到以下错误:Link to image of my error

+1

你能发布不工作的代码吗? – Kevin

+0

与foreach的代码! –

+0

我加了不工作的代码 – Jeroen

回答

0

,如果你使用foreach loop当在渲染HTML看看你会得到这个

<input class="form-control text-box single-line" id="item_IpAdress" name="item.IpAdress" value="" type="text"> 

,当你使用for loop它会是这样

<input class="form-control text-box single-line" id="MpSwitch_0__IpAdress" name="MpSwitch[0].IpAdress" value="" type="text"> 

,因为你正在使用List<MpSwitch>所以当你提交表格for loopMpSwitch[0].IpAdress新值IpAddress将分配到MpSwitchindex 0foreach loop值会被分配到item.IpAdress,并且当您提交List<MpSwitch>时将为null,因为要绑定复杂的对象,您必须为每个项目提供一个索引。你也可以检查这个Model Binding To A List

0

你没有得到的原因是,当你使用一个foreach代替for的,你没有的项目和的索引HTML元素的名称/ ID彼此重叠。查看发送给浏览器的HTML源代码(查看页面源代码或检查元素),并且您会注意到名称为MpSwitch[0].IpAddress等。使用foreach时,它只会是MpSwitch.IpAddress。这不能被序列化为一个集合。

0

MVC ModelBinder需要列表项的索引将它们正确地绑定到ViewModel。

如果使用for循环,则将正确的索引合并到生成的HTML元素的idname属性中。

您也可以明确纳入索引,请参阅Binding arrays with missing elements in asp.net mvc