HTTPPost在我的复杂模型中返回空值

时间:2014-11-07 22:41:06

标签: c# asp.net-mvc asp.net-mvc-4

我在视图中有一个可编辑的复杂模型。在我的视图中,当我单击“保存”时,返回的模型为空或空,“nri_id”值除外。我看了一堆其他帖子并尝试了所有这些但似乎无法让我的工作。我觉得我只是缺少一些简单的东西......

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.nri

@{
ViewBag.Title = "Details";
}

<h2>Details</h2>
@using (Html.BeginForm())
{
@Html.AntiForgeryToken()

<div class="form-horizontal">
    <h4>nri</h4>
    <hr />
    @Html.ValidationSummary(true, "", new { @class = "text-danger" })
    <div class="form-group">
        <div class="col-md-offset-2 col-md-10">
            <input type="submit" value="Save" class="btn btn-default" />
        </div>
    </div>
    <div class="form-group">
        @Html.HiddenFor(model => model.nri_id)
        <dl class="dl-horizontal">
            <dt>
                @Html.LabelFor(model => model.state_id)
            </dt>
            <dd>
                @Html.DisplayFor(model => model.state_id)
            </dd>
             <dt>
                @Html.LabelFor(model => model.ref_number)
            </dt>
            <dd>
                @Html.DisplayFor(model => model.ref_number)
            </dd>
        </dl>
        <h4>@Html.LabelFor(model => model.tasks)</h4>
        <hr />
        @Html.EditorFor(model => model.tasks)
    </div>
</div>
}
    <p>
        @Html.ActionLink("Back to List", "Index")
    </p>

和我的控制员:

    // 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);
    }

修改 将所有@Html.DisplayFor更改为@ Html.TextBoxFor(model =&gt; model.xxxx,new {@readonly =“readonly”})`,这就行了。

我的任务编辑器模板如下。我有一个Hieriachal表,'category',它相应地分组和显示任务。我假设我在某处也需要一个@Html.HiddenFor(model => model.nri_id) ......:

@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>

0 个答案:

没有答案