我有以下控制器和视图。它使用GET正确列出值。当我点击按钮时,它会导致POST。但是,控制器中收到的值为NULL。我们如何纠正它?
突出显示的代码
[HttpPost]
public ActionResult CastVote(IEnumerable<Program> theProgramList)
获取图片
CODE
public enum RatingEnum { Poor = 0, Neutral, Good, Excellent };
public class Program
{
public int ProgramID { get; set; }
public string ProgramName { get; set; }
public RatingEnum RatingID { get; set; }
public string ProgramCategory { get; set; }
}
CONTROLLER
namespace MyProgramRatingApp.Controllers
{
public class ProgramController : Controller
{
List<Program> programList = new List<Program>()
{
new Program
{
ProgramID = 1,ProgramName = "Program1",
ProgramCategory = "A"
},
new Program
{
ProgramID = 2,ProgramName = "Program2",
ProgramCategory = "B"
},
new Program
{
ProgramID = 3,ProgramName = "Program3",
ProgramCategory = "A"
}
};
// GET: /Program/
public ActionResult CastVote()
{
ViewBag.RatingEnum = GetRstingSelectList();
return View(programList);
}
// POST: /StoreManager/Create
[HttpPost]
public ActionResult CastVote(IEnumerable<Program> theProgramList)
{
if (ModelState.IsValid)
{
//Save the book in DB first and then redirectToAction.
return RedirectToAction("CastVote");
}
return View(theProgramList);
}
public static SelectList GetRstingSelectList()
{
Array values = Enum.GetValues(typeof(RatingEnum));
List<System.Web.UI.WebControls.ListItem> items = new List<System.Web.UI.WebControls.ListItem>(values.Length);
foreach (var i in values)
{
items.Add(new System.Web.UI.WebControls.ListItem
{
Text = Enum.GetName(typeof(RatingEnum), i),
Value = ((int)i).ToString()
}
);
}
return new SelectList(items);
}
}
}
查看
@model IEnumerable<MyProgramRatingApp.Program>
@{
ViewBag.Title = "CastVote";
}
<h2>CastVote</h2>
@using (Html.BeginForm())
{
<table>
<tr>
<th style="border:1px solid Teal; background-color:Gray">
ProgramName
</th>
<th style="border:1px solid Teal; background-color:Gray">
RatingID
</th>
<th style="border:1px solid Teal; background-color:Gray">
ProgramCategory
</th>
<th style="border:1px solid Teal; background-color:Gray"></th>
</tr>
@foreach (var item in Model)
{
<tr>
<td style="border:1px solid Teal">
@Html.DisplayFor(modelItem => item.ProgramName)
</td>
<td style="border:1px solid Teal">
@Html.DisplayFor(modelItem => item.RatingID)
</td>
<td style="border:1px solid Teal">
@Html.DisplayFor(modelItem => item.ProgramCategory)
</td>
<td style="border:1px solid Teal">
@Html.DropDownListFor(model => item.RatingID, (SelectList)ViewBag.RatingEnum, String.Empty)
</td>
</tr>
}
</table>
<p>
<input type="submit" value="Cast Vote" />
</p>
}
读:
ASP.NET MVC 3 - Partial vs Display Template vs Editor Template
用于模型绑定到数组,列表,集合,词典的ASP.NET Wire Format
模型绑定到列表
答案 0 :(得分:4)
通过调用编辑器模板替换视图中的foreach
循环:
@model IEnumerable<MyProgramRatingApp.Program>
@{
ViewBag.Title = "CastVote";
}
<h2>CastVote</h2>
@using (Html.BeginForm())
{
<table>
<tr>
<th style="border:1px solid Teal; background-color:Gray">
ProgramName
</th>
<th style="border:1px solid Teal; background-color:Gray">
RatingID
</th>
<th style="border:1px solid Teal; background-color:Gray">
ProgramCategory
</th>
<th style="border:1px solid Teal; background-color:Gray"></th>
</tr>
@Html.EditorForModel()
</table>
<p>
<input type="submit" value="Cast Vote" />
</p>
}
然后定义将为模型中的每个元素自动呈现的编辑器模板(~/Views/Shared/EditorTemplates/Program.cshtml
):
@model MyProgramRatingApp.Program
<tr>
<td style="border:1px solid Teal">
@Html.EditorFor(x => x.ProgramName)
</td>
<td style="border:1px solid Teal">
@Html.EditorFor(x => x.RatingID)
</td>
<td style="border:1px solid Teal">
@Html.EditorFor(x => x.ProgramCategory)
</td>
<td style="border:1px solid Teal">
@Html.DropDownListFor(
x => x.RatingID,
(SelectList)ViewBag.RatingEnum,
String.Empty
)
</td>
</tr>
请注意,我在编辑器模板中使用了@Html.EditorFor
而不是@Html.DisplayFor
来生成输入字段。如果不这样做,则不会在控制器中返回任何值,因为表单不包含任何输入元素。如果您不想显示输入字段,可以使用隐藏的输入:
@model MyProgramRatingApp.Program
<tr>
<td style="border:1px solid Teal">
@Html.DisplayFor(x => x.ProgramName)
@Html.HiddenFor(x => x.ProgramName)
</td>
<td style="border:1px solid Teal">
@Html.DisplayFor(x => x.RatingID)
@Html.HiddenFor(x => x.RatingID)
</td>
<td style="border:1px solid Teal">
@Html.DisplayFor(x => x.ProgramCategory)
@Html.HiddenFor(x => x.ProgramCategory)
</td>
<td style="border:1px solid Teal">
@Html.DropDownListFor(
x => x.RatingID,
(SelectList)ViewBag.RatingEnum,
String.Empty
)
</td>
</tr>
编辑器模板将为输入字段生成正确的名称,以便模型绑定器正确绑定值。
您还可以查看following article,以便更好地了解集合所需的有线格式。
答案 1 :(得分:1)
这段代码对我有用:
2查看:
@model IList<Program>
@using (Html.BeginForm())
{
<table>
<tr>
<th style="border:1px solid Teal; background-color:Gray">
ProgramName
</th>
<th style="border:1px solid Teal; background-color:Gray">
ProgramCategory
</th>
<th style="border:1px solid Teal; background-color:Gray"> </th>
</tr>
@for(var i=0,j=mode.Count;i<j;i++)
{
<tr>
<td>@model[i].ProgrameName</td>
<td><input type="text" name="RaingID[@i]" value="@model[i].RatingID"</td>
<td>
@Html.DropDownListFor(x => x.Rating,
(SelectList)ViewBag.RatingEnum, new {@name="ProgramCategory[i] "})
</td>
</tr>
}
<p>
<input type="submit" value="Cast Vote" />
</p>
</table>
}
答案 2 :(得分:0)
感谢达林。我接受了他的回答。发布完整的解决方案是为了其他人的利益。
注意:EditorTemplate有助于避免foreach循环并帮助正确的对象绑定。
注意:EditorTemplate应放在适当的文件夹中,文件名应基于约定。
//模型
namespace MyProgramRatingApp
{
public enum RatingEnum { Poor = 0, Neutral, Good, Excellent };
public class Program
{
public int ProgramID { get; set; }
public string ProgramName { get; set; }
public RatingEnum Rating { get; set; }
public string ProgramCategory { get; set; }
}
}
//控制器
using System;
using System.Collections.Generic;
using System.Web.Mvc;
namespace MyProgramRatingApp.Controllers
{
public class ProgramController : Controller
{
List<Program> programList = new List<Program>()
{
new Program
{
ProgramID = 1,ProgramName = "Program1",
ProgramCategory = "A"
},
new Program
{
ProgramID = 2,ProgramName = "Program2",
ProgramCategory = "B"
},
new Program
{
ProgramID = 3,ProgramName = "Program3",
ProgramCategory = "A"
}
};
// GET: /Program/
public ActionResult CastVote()
{
ViewBag.RatingEnum = GetRstingSelectList();
return View(programList);
}
// POST: /StoreManager/Create
[HttpPost]
public ActionResult CastVote(IEnumerable<Program> theProgramList)
{
if (ModelState.IsValid)
{
//Save the book in DB first and then redirectToAction.
return RedirectToAction("CastVote");
}
return View(theProgramList);
}
public static SelectList GetRstingSelectList()
{
Array values = Enum.GetValues(typeof(RatingEnum));
List<System.Web.UI.WebControls.ListItem> items = new List<System.Web.UI.WebControls.ListItem>(values.Length);
foreach (var i in values)
{
items.Add(new System.Web.UI.WebControls.ListItem
{
Text = Enum.GetName(typeof(RatingEnum), i),
Value = ((int)i).ToString()
}
);
}
return new SelectList(items);
}
}
}
查看(CastVote.cshtml)
@model IEnumerable<MyProgramRatingApp.Program>
@{
ViewBag.Title = "CastVote";
}
<h2>CastVote</h2>
@using (Html.BeginForm())
{
<table>
<tr>
<th style="border:1px solid Teal; background-color:Gray">
ProgramName
</th>
<th style="border:1px solid Teal; background-color:Gray">
ProgramCategory
</th>
<th style="border:1px solid Teal; background-color:Gray"> </th>
</tr>
@Html.EditorForModel()
</table>
<p>
<input type="submit" value="Cast Vote" />
</p>
}
EditorTemplate(Program.cshtml)
@model MyProgramRatingApp.Program
<tr>
<td style="border:1px solid Teal">
@Html.DisplayFor(x => x.ProgramName)
@Html.HiddenFor(x => x.ProgramName)
</td>
<td style="border:1px solid Teal">
@Html.EditorFor(x => x.ProgramCategory)
</td>
<td style="border:1px solid Teal">
@Html.DropDownListFor(x => x.Rating, (SelectList)ViewBag.RatingEnum, String.Empty)
</td>
<td>
@Html.HiddenFor(x => x.ProgramID)
</td>
</tr>