C# HTTPPost在我的复杂模型中返回空值
我在视图中有一个复杂的模型,它是可编辑的。在我看来,当我单击save时,返回的模型为null或空,除了“nri_id”值。我看过很多其他的帖子,都试过了,但似乎都没法让我的帖子发挥作用。我觉得我只是错过了一些简单的事情C# HTTPPost在我的复杂模型中返回空值,c#,asp.net-mvc,asp.net-mvc-4,C#,Asp.net Mvc,Asp.net Mvc 4,我在视图中有一个复杂的模型,它是可编辑的。在我看来,当我单击save时,返回的模型为null或空,除了“nri_id”值。我看过很多其他的帖子,都试过了,但似乎都没法让我的帖子发挥作用。我觉得我只是错过了一些简单的事情 public partial class nri { public nri() { tasks = new HashSet<task>(); } [Key] public int nri_id { get; s
public partial class nri
{
public nri()
{
tasks = new HashSet<task>();
}
[Key]
public int nri_id { get; set; }
public int ref_number { get; set; }
public int state_id { get; set; }
[UIHint("_taskList")]
public virtual ICollection<task> tasks { get; set; }
}
和我的任务模板:
@model NSCEngineering.Models.task
<tr>
@Html.HiddenFor(model => model.task_id)
<td>
@Html.DisplayFor(model => model.task_name)
</td>
<td>
@Html.DisplayFor(model => model.task_desc)
</td>
<td>
@Html.DropDownListFor(model => model.task_state_id, new SelectList((System.Collections.IEnumerable)ViewData["TaskStates"], "task_state_id", "state"))
</td>
<td>
@Html.DisplayFor(model => model.user_completed)
</td>
<td>
@Html.DisplayFor(model => model.completion_date)
</td>
<td>
@Html.EditorFor(model => model.notes, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.notes, "", new { @class = "text-danger" })
</td>
</tr>
@model.engineering.Models.task
@Html.HiddenFor(model=>model.task_id)
@DisplayFor(model=>model.task_name)
@DisplayFor(model=>model.task_desc)
@Html.DropDownListFor(model=>model.task\u state\u id,新选择列表((System.Collections.IEnumerable)ViewData[“TaskStates”],“task\u state\u id”,“state”))
@DisplayFor(model=>model.user\u已完成)
@DisplayFor(model=>model.completion\u日期)
@EditorFor(model=>model.notes,new{htmlAttributes=new{@class=“form control”})
@Html.ValidationMessageFor(model=>model.notes,“,new{@class=“text danger”})
@Html.DisplayFor()
不会创建回发值的控件。您需要使用TextBoxFor()
或HiddenFor()
作为要回发的属性(正如您对nri\u id
属性所做的那样)“任务”是什么?或者使用EditorFor代替DisplayFor@StephenMuecke !! 这解决了一个问题。添加new{@readonly=“readonly”}
是使其只读的安全方法吗?@Eru请查看任务编辑器的“我的编辑”for。谢谢。是的,您可以这样做,或者使用@HiddenFor()
为属性添加一个隐藏输入,但是包含大量隐藏输入不是一个好的做法-如果您不编辑它们,从数据库中获取对象并更新所需的属性通常比通过网络发布大量额外数据的性能更好
// GET: nris/Details/5
[HttpGet]
public async Task<ActionResult> Details(int id = 0)
{
nri n = await _nriRepository.GetNRIByID(id);
return View(n);
}
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Details(nri nriModel)
{
try
{
if (ModelState.IsValid)
{
_nriRepository.UpdateNRI(nriModel);
_nriRepository.SaveAll();
return View(nriModel);
}
}
catch (DataException)
{
ModelState.AddModelError("", "Unable to save changes. Try again, and if the problem persists see your system administrator.");
}
return View(nriModel);
}
@model IEnumerable<NSCEngineering.Models.task>
<div class="form-group">
@{var categories = ViewData["AllCategories"] as List<NSCEngineering.Models.category>;}
@for (int i=0; i < (categories.Where(x=>x.parent_category_id ==null)).Count(); i++)
{
<dl>
<dt>@Html.DisplayFor(x => categories[i].category_name)</dt>
@{IEnumerable<NSCEngineering.Models.task> CategoryTasks = Model.Where(z => z.category_id == categories[i].category_id);}
@foreach (var task in CategoryTasks) {
<dd>
@Html.EditorFor(x => task, "_task")
</dd>
}
@*}*@
@for (int j = 0; j < categories[i].Subcategories.Count(); j++ )
{
<dd>
<dl>
<dt>
@Html.DisplayFor(x => categories[i].Subcategories[j].category_name)
</dt>
@{IEnumerable<NSCEngineering.Models.task> subTasks = Model.Where(task => task.category_id == categories[i].Subcategories[j].category_id); }
@foreach (var subtask in subTasks)
{
<dd>
@Html.EditorFor(x => subtask, "_task")
</dd>
}
</dl>
</dd>
}
</dl>
}
@model NSCEngineering.Models.task
<tr>
@Html.HiddenFor(model => model.task_id)
<td>
@Html.DisplayFor(model => model.task_name)
</td>
<td>
@Html.DisplayFor(model => model.task_desc)
</td>
<td>
@Html.DropDownListFor(model => model.task_state_id, new SelectList((System.Collections.IEnumerable)ViewData["TaskStates"], "task_state_id", "state"))
</td>
<td>
@Html.DisplayFor(model => model.user_completed)
</td>
<td>
@Html.DisplayFor(model => model.completion_date)
</td>
<td>
@Html.EditorFor(model => model.notes, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.notes, "", new { @class = "text-danger" })
</td>
</tr>