1

我的应用程序有问题。当用户动态添加行(javascript)以向“声明”添加更多“声明行”时,发布的值不会出现,只有我最初设置 ViewModel 时使用的“虚拟”值。因此,基本上用户修改或添加的任何内容(新行)都不会记录在 POST 上。

据我所见,我正在控制器和视图之间传递 ViewModel,但显然我遗漏了一些东西。我已经为此苦苦挣扎了一段时间,非常感谢您的帮助。我是 ASP.NET MVC 和一般编程的新手。我正在关注 Steven Sanderson 的博客。

视图模型

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using ef_tut.Models;
using ef_tut.ViewModels;
namespace ef_tut.ViewModels
{
    public class ClaimViewModel
    {
        public int ClaimID { get; set; }
        public int SubmissionUserID { get; set; }
        public DateTime? DateSubmitted { get; set; }
        public bool ApprovedYN { get; set; }
        public DateTime? DateApproved { get; set; }

        public ICollection<claimline> claimlines { get; set; }

    }
}

控制器

  using System;
    using System.Collections.Generic;
    using System.Data;
    using System.Data.Entity;
    using System.Linq;
    using System.Web;
    using System.Web.Mvc;
    using ef_tut.Models;
    using ef_tut.DAL;
    using ef_tut.ViewModels;
    using ef_tut.WebUI.Helpers;
    namespace ef_tut.Controllers
    {
        public class ClaimsController : Controller
        {
            private ClaimContext db = new ClaimContext();

            private static ClaimViewModel _currentclaim;
            private static ClaimViewModel Currentclaim
            {
                get
                {
                    _currentclaim = GetClaimViewModel();
                    return _currentclaim;
                }
                set
                {
                    _currentclaim = value;
                }
            }

            public static ClaimViewModel GetClaimViewModel()
            {
                return new ClaimViewModel()
            {
                ClaimID =101,
                claimlines = new List<claimline>() {
                        new claimline() {ClaimantUserID =1},
                        new claimline() {ClaimantUserID =2},
                        new claimline() {ClaimantUserID =3}
                    }
            };}

            public ActionResult Create()
            {
                return View(Currentclaim);
            }

            public PartialViewResult BlankEditorRow()
            {


                return PartialView("NewRow");
            }

            [HttpPost]
            public ActionResult Create(ClaimViewModel ClaimViewModel)
            {
                Currentclaim = ClaimViewModel;

                return View("View2", Currentclaim);
            }
        }
    }

创建视图

@model ef_tut.ViewModels.ClaimViewModel
@using ef_tut.Models
@using ef_tut.ViewModels
@{
    ViewBag.Title = "Create";
}

<h2>Create</h2>

@using (Html.BeginForm()) {
    @Html.ValidationSummary(true)

    <fieldset>
        <legend>claim</legend>
                      @Html.HiddenFor(model => model.ClaimID)

        <ul id="editorRows">


                            @foreach (claimline claimline in Model.claimlines)
                            {
                                Html.RenderPartial("NewRow", claimline);
                }

        </ul> 


        <p>
               @Html.ActionLink("Add another...", "BlankEditorRow", null, new { id = "addItem" }) 

        </p>
    </fieldset>
             <input type="submit" value="Create" />



@section Scripts {
    @Scripts.Render("~/bundles/jqueryval")
}
}   

部分的

@model ef_tut.Models.claimline

@using ef_tut.WebUI.Helpers
    @using (Html.BeginCollectionItem("claims"))
    {
        <table class="editorRow">
   <tr >

                                                     <td>
            ClaimID(claimlinetable)@Html.EditorFor(model => model.ClaimID)
            </td>

       <td>
          ClaimantUserID:  @Html.EditorFor(model => model.ClaimantUserID)
            </td>
                               <td>
           Hours: @Html.EditorFor(model => model.Hours)
            </td>
                                                          <td>
           <a href="#" class="deleteRow">delete</a>
            </td>
               </tr></table>
    }

输出视图

@model ef_tut.ViewModels.ClaimViewModel
@using ef_tut.Models

@{
    ViewBag.Title = "View2";
}

<h2>View1</h2>

@Html.HiddenFor(model=>model.ClaimID)
    <ul>
        @foreach (claimline claimline in Model.claimlines){ 
            @claimline.ClaimantUserID; }
    </ul>
4

1 回答 1

1

您需要使用 BeginCollectionItem 助手,参数 collectionName 等于视图模型中的集合名称(声明行)。这是因为这个助手替换了 HtmlFieldPrefix 并且为 html 字段生成的名称是:specified_collection_name[generated_index].claimline_property_name

因此,在您的情况下,为输入生成的名称如下:claims[generated_index].Hours

但是 mvc 模型绑定器在 ClaimViewModel 中找不到属性声明。

如果将 @using (Html.BeginCollectionItem("claims")) 替换为 @using (Html.BeginCollectionItem("claimlines")) 生成的输入名称将如下所示:

索赔线[生成索引].小时

并且模型绑定器将按照您的预期生成 ClaimViewModel

于 2013-10-06T22:55:13.433 回答