2017-10-06 13 views
0

部分的なビューにフォームを含む部分ビューを含む基本的な.Net MVCページがあります。フォームは、フルページのモデルの一部で構成されています。これは期待どおりに動作します。私が抱えている問題は、部分的にフォームを提出することは、部分的にモデルをそのアクションに戻すことではなく、それはヌルになるということです。このページにはいくつかの単純な部分があり、それは期待通りに機能しません。ASP.NetのフォームMVCの部分ページがモデルをアクション/メソッドに投稿していません

ビューモデル:

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) 
+0

は 'link'の完全ヌルまたはそれのプロパティの一部ですヌルありますか? –

+0

ポストバックでは完全にnullです。取得では、それが予想通りに設定されています –

+0

メインビューを表示できますか? – Win

答えて

2

パラメータ 'link'の名前を 'model'などに変更してください。この記事を参照してください。

viewmodel returns null on postback mvc 5

+1

いいキャッチ.....! –

+0

はい非常に良いキャッチ! –

関連する問題