如何通过表单获取IList <model>的值并将其传递给MVC中的控制器

时间:2016-05-18 15:06:19

标签: asp.net asp.net-mvc list razor ienumerable

我过去两天都在摸不着头脑,包括我正在努力建立的信息。

我正在尝试构建的功能: 简单的旅行经理申请

获取索引页面上的旅行名称和人数的值并提交它,这将导致另一种形式(视图),其中将根据将生成文本框的人数将该表单数据传递给另一个我将保存的控制器它进入数据库。

型号: 的旅行

public class Trip
    {
        public Trip()
        {
            //people = new List<People>();
        }
        public int Id { get; set; }
        public string Name { get; set; }
        public string Description { get; set; }
        public decimal Income { get; set; }
        public decimal Expense { get; set; }
        public int NumberOfPeople { get; set; }
        public virtual IList<People> people{ get; set; }
    }

public class People
    {
        public int Id { get; set; }
        public int TripId { get; set; }
        [DisplayName("Name:")]
        public string Name { get; set; }
        [DisplayName("Initial Deposite:")]
        public decimal InitialDeposite { get; set; }


    }

这就是Index.cshtml View的样子,这里我采取了两个输入名称和人数:

@model TripManager.Models.Trip
@{
    ViewBag.Title = "Welcome to Trip Manager";
}

<div class="jumbotron">
    <h1>Trip Manager</h1>
    <p class="lead">Trip Manager helps you calculate your group trip expenses in ease.</p>
</div>

<div class="row">
    <div class="col-md-12 text-center">
            @using (Html.BeginForm("Index","Trip"))
            {

                @Html.AntiForgeryToken();
                @*@Html.HiddenFor(m=>m.Id)*@
                <p class="text-info">@Html.LabelFor(m => m.Name)</p>
                <p class="glyphicon-search">@Html.EditorFor(m => m.Name) @Html.ValidationMessageFor(m => m.Name)</p>


                <p class="text-info">@Html.LabelFor(m => m.NumberOfPeople)</p>
                <p class="glyphicon-search">@Html.EditorFor(m => m.NumberOfPeople) @Html.ValidationMessageFor(m => m.NumberOfPeople)</p>
                <p><input type = "submit" class="btn-success" name="btnTripInit" value="Go Trip" /></p>
            }
    </div>
</div>

将这些输入传递给member.cshtml通过控制器发布操作索引(Trip trip)查看,如下所示,

public ActionResult Index(Trip trip)
{
    if (!ModelState.IsValid)
    {
        return View(trip);
    }

    var mytrip = new Trip();
    mytrip.Name = trip.Name;
    mytrip.NumberOfPeople = trip.NumberOfPeople;
    mytrip.people = new List<People>(trip.NumberOfPeople);

    return View("Members", trip);
}

和members.cshtml视图如下所示...这里我在Trip Model中为IList生成文本框....一个名称的文本框和Initial Deposite的其他文本

@model TripManager.Models.Trip
{
   @ViewBag.Title = "Please Enter Members Details";
}
<div class="row">
    <div class="col-md-12 text-center">
      @{
          using (Html.BeginForm("FeedMemberData", "Trip",FormMethod.Post))
          {
            <hr />
            @Html.HiddenFor(m=>m.Id)
              for(int i=0;i<Model.NumberOfPeople;i++)
              {
                <p>Member @i Details</p>
                @Html.HiddenFor(m => m.people[i].Id)
                <p class="text-info">@Html.LabelFor(m=>m.people[i].Name) @Html.EditorFor(m => m.people[i].Name , new {@class="glyphicon-search"})
                 | @Html.LabelFor(m => m.people[i].InitialDeposite) @Html.EditorFor(m => m.people[i].InitialDeposite , new { @class = "glyphicon-search" })
                </p>
              }
            <hr />
            <p><input type="submit" class="btn-success" name="btnMemberInit" value="Save Details" /></p>
          }
    }
    </div>
</div>

现在我应该如何将此表单数据发送到控制器并通过EntityFramework ???将其保存到DB

public ActionResult FeedMemberData(Trip trip)
{
    //TODO
}

这里旅行我得到了空......

2 个答案:

答案 0 :(得分:0)

如果您希望发布到控制器方法的Trip对象为null,我发现它通常是由于html页面没有正确发布数据或者MVC模型绑定器无法正确解析将数据格式化为模型,在本例中为Trip对象,您希望将其发布到服务器。

如果我猜测,您发布的方法是什么&#34; FeedMemberData&#34;实际上是一个People数组,而不是您用作模型的Trip对象。请尝试以下操作,查看模型绑定器是否将发布数据绑定到List。

[HttpPost]
public ActionResult FeedMemberData(List<People> people)
{
    //TODO
}

答案 1 :(得分:0)

后来发现了问题,做了一些改变并且有效。

一般注意事项:

如果您遇到像Ienumerable这样的问题,将null返回给Controller,请确保在向IList提供数据的同时从0开始索引

@model TripManager.Models.Trip
{
   @ViewBag.Title = "Please Enter Members Details";
}
<div class="row">
    <div class="col-md-12 text-center">
      @{
          using (Html.BeginForm("FeedMemberData", "Trip",FormMethod.Post))
          {
            <hr />
            @Html.HiddenFor(m=>m.Id)
             //this must start from 0
              for(int i=0;i<Model.NumberOfPeople;i++)
              {
                <p>Member @i Details</p>
                @Html.HiddenFor(m => m.people[i].Id)
                <p class="text-info">@Html.LabelFor(m=>m.people[i].Name) @Html.EditorFor(m => m.people[i].Name , new {@class="glyphicon-search"})
                 | @Html.LabelFor(m => m.people[i].InitialDeposite) @Html.EditorFor(m => m.people[i].InitialDeposite , new { @class = "glyphicon-search" })
                </p>
              }
            <hr />
            <p><input type="submit" class="btn-success" name="btnMemberInit" value="Save Details" /></p>
          }
    }
    </div>
</div>