繁体   English   中英

HTTPPost上的ViewModel为Null

[英]ViewModel is Null on HTTPPost

我是MVC的新手,现在已经为这个问题苦苦挣扎了几天。

当我将表单发布回服务器时,这些值始终为null。 我尝试使用模型本身,使用集合/列表,最后尝试的方法是使用ViewModel。

我要实现的目标是标记用户注册的事件的出席率。 我正在获取正确的出席信息并将其发送到视图。 我将选中复选框以更新布尔值Attend.Attended。 在调试期间,我将在Post操作的开始处放置一个断点,并且模型,集合/列表和ViewModel每次都为null。

楷模:

public class Attend
{
  [Key]
  public int AttendID { get; set; }

  public virtual UserProfile User { get; set; }

  public virtual Event Event { get; set; }

  public Boolean SignedUp { get; set; }

  public Boolean Attended {get; set; }

}

public class Event
{
  [Key]
  public long EventID { get; set; }

  [Required]
  [DisplayName("When is this event?")]
  public DateTime DateScheduled { get; set; }

  public DateTime DateCreated { get; set; }

  [DisplayName("Event Category")]
  public String Category { get; set; }

  [Required]
  [DisplayName("Location")]
  public String Location { get; set; }

  public string Comments { get; set; }

  [Required]
  [DisplayName("Event Name")]
  public string EventName { get; set; }

  [Required]
  [DisplayName("Event Description")]
  public string EventDescription { get; set; }

  public virtual ICollection<Attend> Attends { get; set; }
}

控制器:

    //
    // GET: /Event/Attendance
    [HttpGet]
    public ActionResult Attendance(long id)
    {
        try
        {
            var model = new AttendanceViewModel();

            if (db == null)
                return HttpNotFound();

            if (Request.UrlReferrer != null && Request.UrlReferrer.AbsoluteUri != null)
                ViewBag.ReferrerUrl = Request.UrlReferrer.AbsoluteUri;
            else
                ViewBag.ReferrerUrl = Url.Action("Index");

            model.Attending = db.Attends.ToList();

            ViewBag.myID = id;
            return View(model);
        }
        catch (Exception ex)
        {
            Log.Error(ex.Message, ex);
            return HttpNotFound();
        }
    }

    //
    // POST: /Event/Attendance
    [HttpPost]
    public ActionResult Attendance(AttendanceViewModel Attending, long id)
    {

       //POST ACTION...
    }

视图:

model CottagesOfHope.ViewModels.AttendanceViewModel

@{
    ViewBag.Title = "Attendance";
}

<h2>Mark Attendance</h2>


@using (Html.BeginForm())
{
    <fieldset>
        <legend>Attendance</legend>
        <table>
            <thead>
                <tr>
                    <th>Name</th>
                    <th>Attendance</th>
                </tr>
            </thead>
            <tbody>
                @foreach (var j in Model.Attending)
                {
                    if (j.Event.EventID == ViewBag.myId)
                    {
                        <tr>
                           <td>@j.User.FirstName @j.User.LastName</td>
                           <td>@Html.EditorFor(model => j.Attended)</td>
                       </tr>
                    }
                }
            </tbody>
        </table>
        <p>
            <input type="submit" value="Submit" />
        </p>
    </fieldset>
}

ViewModel:

public class AttendanceViewModel
{
    public virtual List<Attend> Attending { get; set; }
}

如我之前所说,这是我尝试正确绑定数据的最后一种方法。 任何帮助将不胜感激。
提前致谢!

看起来您没有将任何必需的参数传递给BeginForm方法,请尝试以下操作:

@using (Html.BeginForm("ActionName", "ControllerName", FormMethod.Post)) {...}

代替这个:

@using (Html.BeginForm()) {...}

其中"ControllerName""ControllerName"的名称,而"ActionName"是控制器动作的名称。 在这里查看更多。

如果不指定参数,则生成的html如下所示:

<form action="/" method="post"></form>

但是,当您确实指定参数时,html将如下所示:

<form action="/ControllerName/ActionName" method="post"></form>

因此实际上存在两个问题:

  1. 我应该在Controller中过滤列表时,才在View中过滤列表。
  2. 我读到,foreach循环有时无法在列表上正常工作,建议使用for循环并在每次迭代中为List编制索引。

更新的控制器:

[HttpPost]
    public ActionResult Attendance(ViewModels.AttendanceViewModel a)
    {
        try
        {
        foreach (var j in a.Attending)
        {
            //Needed to filter by EventID here
            Attend attends = db.Attends.Where(e => e.AttendID == j.AttendID).Single();

            attends.Attended = j.Attended;
        }
        db.SaveChanges();
        return RedirectToAction("Index", "Event");
        }
        catch (Exception ex)
        {
            Log.Error(ex.Message, ex);
            return HttpNotFound();
        }
    }

更新视图:

@model CottagesOfHope.ViewModels.AttendanceViewModel

@{
   ViewBag.Title = "Attendance";
 }

<h2>Mark Attendance</h2>


@using (Html.BeginForm())
{
<fieldset>
    <legend>Attendance</legend>
    <table>
        <thead>
            <tr>
                <th>Name</th>
                <th>Attendance</th>
            </tr>
        </thead>
        <tbody>
            @for (int i = 0; i < Model.Attending.Count(); i++)
            {
                <tr>
                       <td>@Model.Attending[i].User.FirstName 
                           @Model.Attending[i].User.LastName</td>
                       <td>@Html.CheckBoxFor(model => Model.Attending[i].Attended)</td>
                        @Html.HiddenFor(model => Model.Attending[i].AttendID)
                   </tr>
            }
        </tbody>
    </table>
    <p>
        <input type="submit" value="Submit" />
    </p>
</fieldset>
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM