在ASP.Net MVC部分页面中表单而不是将模型发布到Action / Method

时间:2017-10-06 18:10:49

标签: c# asp.net-mvc

我有一个基本的.Net MVC页面,其中包含局部视图中带有表单的局部视图。表单由整页模型的一部分组成。这按预期工作。我遇到的问题是,在partial上提交表单并没有将模型传递给部分返回到action,它只是null。我在这个页面上有几个简单的部分按预期工作,只有这个部分不再有效。

视图模型:

 public class LinkVM 
    {
    public int ProjectID { get; set; }
    public string Link { get; set; }
    public int SelectedLinkTypeId { get; set; }
    [Display(Name ="Link Note")]
    public string Notes { get; set; }
    [Display(Name = "Link Type")]
    public LinkTypeVM LinkType { get; set; }
    public IEnumerable<SelectListItem> LinkTypes { get; set; }
    }

部分视图:

@model xxxx.ViewModel.LinkVM

<div id="dialog-link" class="frm" title="Add Link">
    @using (Html.BeginForm("CreateLink", "Projects", FormMethod.Post, new { id = "CreateLink" }))
    {
        @Html.HiddenFor(a => a.ProjectID)
        <fieldset>
            @Html.LabelFor(a => a.LinkType, new { @class = "label radius" })
            @Html.DropDownListFor(a => a.SelectedLinkTypeId, Model.LinkTypes, "Select Link Type", htmlAttributes: new { @class = "" })
            @Html.ValidationMessageFor(a => a.SelectedLinkTypeId)

            @Html.LabelFor(a => a.Link, new { @class = "label radius" })
            @Html.TextBoxFor(a => a.Link, new { @class = "" })
            @Html.ValidationMessageFor(a => a.Link)

            @Html.LabelFor(a => a.Notes, new { @class = "label radius" })
            @Html.TextAreaFor(a => a.Notes, 5, 55, null)
            @Html.ValidationMessageFor(a => a.Notes)
            <input type="submit" />
        </fieldset>
    }
</div>

我的行动:

[HttpPost]
public ActionResult CreateLink(LinkVM link)
    {
        if (link.ProjectID < 1) {
            throw new Exception();
        }
        link.CreateDate = DateTime.Now;
        link.CreatedBy = HttpContext.User.Identity.Name.NameFromADName();
        link.UpdatedBy = HttpContext.User.Identity.Name.NameFromADName();
        db.ProjectLinks.Add(link.Adapt<ProjectLink>());
        db.SaveChanges();

        return RedirectToAction("edit", "Projects", new { @id = link.ProjectID });
    }

主要观点:

 @model xxxx.ViewModel.ProjectVM


@using (Html.BeginForm())
{
    @Html.AntiForgeryToken()
    <div class="row">
        <div class="medium-6 column end">
            <h4>Edit Project # @Html.DisplayFor(m => m.ProjectNumber)</h4>
            @Html.ValidationSummary(true, "", new { @class = "text-danger" })
            @Html.HiddenFor(model => model.Id)
            @Html.HiddenFor(model => model.ProjectNumber)
        </div>
    </div>
    <div class="row">
        <div class="medium-4 end column">
            @Html.LabelFor(model => model.SelectedProjectTypeId, htmlAttributes: new { @class = "label radius " })
            @Html.DropDownListFor(m => m.SelectedProjectTypeId, Model.ProjectTypes, "Select Project Type", htmlAttributes: new { @class = "ddl" })
            @Html.ValidationMessageFor(model => model.SelectedProjectTypeId, "", new { @class = "text-danger" })
        </div>
    </div>
    <br />
    <div class="row">
        <div class="medium-3  column end">
            @Html.LabelFor(model => model.Priority, htmlAttributes: new { @class = "label radius" })
            @Html.TextBoxFor(model => model.Priority, new { htmlAttributes = new { @class = "" } })
            @Html.ValidationMessageFor(model => model.Priority, "", new { @class = "text-danger" })
        </div>
    </div>
    <div class="row">
        <div class="medium-6  column end">
            @Html.LabelFor(model => model.IsOverride, htmlAttributes: new { @class = "label radius" })
            <div class="checkbox">
                @Html.EditorFor(model => model.IsOverride)
                @Html.ValidationMessageFor(model => model.IsOverride, "", new { @class = "text-danger" })
            </div>
        </div>
    </div>
    <div class="row">
        <div class="medium-3  column end">
            @Html.LabelFor(model => model.SubmittedDate, htmlAttributes: new { @class = "label  radius " })
            @Html.TextBoxFor(model => model.SubmittedDate, "{0:MM/dd/yyyy}", new { htmlAttributes = new { @class = "" } })
            @Html.ValidationMessageFor(model => model.SubmittedDate, "", new { @class = "text-danger" })
        </div>
    </div>
    <div class="row">
        <div class="medium-3  column end">
            @Html.LabelFor(model => model.RequestedCompletionDate, htmlAttributes: new { @class = "label radius " })
            @Html.TextBoxFor(model => model.RequestedCompletionDate, "{0:MM/dd/yyyy}", new { htmlAttributes = new { @class = "" } })
            @Html.ValidationMessageFor(model => model.RequestedCompletionDate, "", new { @class = "text-danger" })
        </div>
    </div>
    <div class="row">
        <div class="medium-3  column end">
            @Html.LabelFor(model => model.MandatoryCompletionDate, htmlAttributes: new { @class = "label radius " })
            @Html.TextBoxFor(model => model.MandatoryCompletionDate, "{0:MM/dd/yyyy}", new { htmlAttributes = new { @class = "" } })
            @Html.ValidationMessageFor(model => model.MandatoryCompletionDate, "", new { @class = "text-danger" })
        </div>
    </div>
    <div class="row">
        <div class="medium-4 end  column">
            @Html.LabelFor(model => model.SelectedMandateSourceId, htmlAttributes: new { @class = "label radius " })
            @Html.DropDownListFor(m => m.SelectedMandateSourceId, Model.MandateSources, "Select Source", htmlAttributes: new { @class = "ddl" })
            @Html.ValidationMessageFor(model => model.SelectedMandateSourceId, "", new { @class = "text-danger" })
        </div>
    </div>
    <br />

    <div class="row">
        <div class="medium-6  column end">
            @Html.LabelFor(model => model.RiskAssessment, htmlAttributes: new { @class = "label  radius" })
            @Html.EditorFor(model => model.RiskAssessment, new { htmlAttributes = new { @class = "" } })
            @Html.ValidationMessageFor(model => model.RiskAssessment, "", new { @class = "text-danger" })
        </div>
    </div>
    <div class="row">
        <div class="medium-3  column end">
            @Html.LabelFor(model => model.ActualStartDate, htmlAttributes: new { @class = "label  radius" })
            @Html.TextBoxFor(model => model.ActualStartDate, "{0:MM/dd/yyyy}", new { htmlAttributes = new { @class = "" } })
            @Html.ValidationMessageFor(model => model.ActualStartDate, "", new { @class = "text-danger" })
        </div>
    </div>
    <div class="row">
        <div class="medium-3  column end">
            @Html.LabelFor(model => model.ActualCompletionDate, htmlAttributes: new { @class = "label  radius" })
            @Html.TextBoxFor(model => model.ActualCompletionDate, "{0:MM/dd/yyyy}", new { htmlAttributes = new { @class = "" } })
            @Html.ValidationMessageFor(model => model.ActualCompletionDate, "", new { @class = "text-danger" })
        </div>
    </div>
    <div class="row">
        <div class="medium-6  column end">
            @Html.LabelFor(model => model.IsActive, htmlAttributes: new { @class = "label  radius" })
            <div class="checkbox">
                @Html.EditorFor(model => model.IsActive)
                @Html.ValidationMessageFor(model => model.IsActive, "", new { @class = "text-danger" })
            </div>
        </div>
    </div>
    <div class="row">
        <div id="accordion" class="medium-12 column">
            <h3>Project Notes</h3>
            <div class="row" id="ProjectNotes">
                <fieldset class="medium-12  column fieldset">
                    <legend>Project Notes</legend>
                    <div class="row">
                        <div class="medium-10 column">
                            <table class="table" role="grid" style="width:100%;">
                                <thead>
                                    <tr>
                                        <th></th>
                                        <th>
                                            @Html.DisplayNameFor(a => a.Notes.First().ProjectNoteID)
                                        </th>
                                        <th>
                                            @Html.DisplayNameFor(a => a.Notes.First().UpdateDate)
                                        </th>
                                        <th>
                                            @Html.DisplayNameFor(a => a.Notes.First().Notes)
                                        </th>
                                    </tr>
                                </thead>
                                <tbody>
                                    @foreach (var item in Model.Notes)
                                    {
                                        <tr>
                                            <td>
                                                <a href="@Url.Action("Edit", "Notes", new { id = item.ProjectNoteID })" class=""><i class="fa fa-edit fa-1x"></i></a>
                                            </td>
                                            <td>
                                                @Html.DisplayFor(a => item.ProjectNoteID)
                                            </td>
                                            <td>
                                                @Html.DisplayFor(a => item.UpdateDate)
                                            </td>
                                            <td>
                                                @if (!String.IsNullOrEmpty(item.Notes))
                                                {
                                                    <span data-tooltip aria-haspopup="true" class="has-tip" title="@item.Notes"><i class="fa fa-info-circle fa-1x"></i></span>
                                                }
                                            </td>
                                        </tr>
                                    }
                                </tbody>
                            </table>
                        </div>
                        <div class="medium-2 column small-text-center">
                            <div id="create-note" class="button tiny radius"><i class="fa fa-edit"></i>&nbsp;Add Note</div>
                        </div>
                    </div>
                </fieldset>
            </div>

            <h3>Project Links</h3>
            <div class="row" id="ProjectLinks">
                <fieldset class="medium-12  column fieldset">
                    <legend>Project Links</legend>
                    <div class="row">
                        <div class="medium-10 column">
                            <table class="table" role="grid" style="width:100%;">
                                <thead>
                                    <tr>
                                        <th></th>
                                        <th>
                                            @Html.DisplayNameFor(a => a.Links.First().Id)
                                        </th>
                                        <th>
                                            @Html.DisplayNameFor(a => a.Links.First().LinkType)
                                        </th>
                                        <th>
                                            @Html.DisplayNameFor(a => a.Links.First().Link)
                                        </th>
                                        <th>
                                            @Html.DisplayNameFor(a => a.Links.First().Notes)
                                        </th>
                                    </tr>
                                </thead>
                                <tbody>
                                    @foreach (var item in Model.Links)
                                    {
                                        <tr>
                                            <td>
                                                <a href="@Url.Action("Edit", "Links", new { id = item.Id })" class=""><i class="fa fa-edit fa-1x"></i></a>
                                            </td>
                                            <td>
                                                @Html.DisplayFor(a => item.Id)
                                            </td>
                                            <td>
                                                @Html.DisplayFor(a => item.LinkType.Name)
                                            </td>
                                            <td>
                                                <a href="@item.Link" target="_blank">@item.Link</a>
                                            </td>
                                            <td>
                                                @if (!String.IsNullOrEmpty(item.Notes))
                                                {
                                                    <span data-tooltip aria-haspopup="true" class="has-tip" title="@item.Notes"><i class="fa fa-info-circle fa-1x"></i></span>
                                                }
                                            </td>
                                        </tr>
                                    }
                                </tbody>
                            </table>
                        </div>
                        <div class="medium-2 column small-text-center">
                            <div id="create-link" class="button tiny radius"><i class="fa fa-link"></i>&nbsp;Add Link</div>
                            @*<a href="@Url.Action("create","Links")" class="button tiny radius "><i class="fa fa-link"></i>&nbsp;Add Link</a>*@
                        </div>
                    </div>
                </fieldset>
            </div>
    <br />
    <div class="row">
        <div class="medium-offset-1" medium-4 columns end">
            <ul class="button-group radius">
                <li>
                    <a href="@Url.Action("index","Projects")" class="button info small"><i class="fa fa-undo fa-1x"></i> Cancel</a>
                </li>
                <li>
                    <button type="submit" value="Create" class="button radius success small"><i class="fa fa-save fa-1x"></i> Save</button>
                </li>
            </ul>
        </div>
    </div>
}

@Html.Partial("_AddNote",Model.Note)
@Html.Partial("_AddLink", Model.Link)

1 个答案:

答案 0 :(得分:2)

尝试将参数'link'重命名为'model'或其他内容。看这篇文章:

viewmodel returns null on postback mvc 5