模板在表单发布到控制器时始终为NULL

时间:2016-07-12 06:38:04

标签: asp.net asp.net-mvc

每当我提交表单时,传递给控制器​​的模型都是NULL。我花了很长时间看这个。我想我在这里缺少一些基本的东西。

(function(window) {
  var recursion = function(str, index, canReverse) {
  var index = !index ? 0 : index,
      result = str.substring(0, index),
      len = str.length;
    console.log(result);
    if (index < len && !canReverse) {
      recursion(str, ++index);
    } else if (index > 0) { // reverse
      recursion(str, --index, true);
    }
  };
  recursion('A joke')
})(window);

模型是:

@model VisitorPortal.Models.ReinviteVisitorModel
@using (Html.BeginForm("CreateMeeting", "Home", FormMethod.Post, new { @class = "form-horizontal", role = "form" }))
{
    @Html.AntiForgeryToken()
    <h3>Reinvitation Details</h3>
    <div>The information entered below will be sent to the visitors email address @Model.Info.Email</div>
    <hr />
    @Html.ValidationSummary("", new { @class = "text-danger" })
    <div class="form-group">
        @Html.LabelFor(m => m.NewMeeting.Title, new { @class = "col-md-2 control-label" })
        <div class="col-md-10">
            @Html.TextBoxFor(m => m.NewMeeting.Title, new { @class = "form-control" })
        </div>
    </div>
    <div class="form-group">
        @Html.LabelFor(m => m.NewMeeting.StartTime, new { @class = "col-md-2 control-label" })
        <div class="col-md-10">
            @Html.TextBoxFor(m => m.NewMeeting.StartTime, new { @class = "datetimepicker form-control" })
        </div>
    </div>
    <div class="form-group">
        @Html.LabelFor(m => m.NewMeeting.EndTime, new { @class = "col-md-2 control-label" })
        <div class="col-md-10">
            @Html.TextBoxFor(m => m.NewMeeting.EndTime, new { @class = "datetimepicker form-control" })
        </div>
    </div>
    <div class="form-group">
        <div class="col-md-offset-2 col-md-10">
            @Html.HiddenFor(m => m.NewMeeting.SubjectId, new { @Value = Model.Info.SubjectId })
            <input type="submit" class="btn btn-default" value="Send Invite" />
        </div>
    </div>
}

Controller操作是:

public class Meeting
{
    [Key]
    public int Id { get; set; }

    public string SubjectId { get; set; }

    [Required]
    [Display(Name = "Reason for invitation")]
    public string Title { get; set; }

    [Required]
    [Display(Name = "Start Time")]
    [DataType(DataType.Time)]
    public DateTime StartTime { get; set; }

    [Required]
    [Display(Name = "End Time")]
    [DataType(DataType.Time)]
    public DateTime EndTime { get; set; }

    public string HostEmail { get; set; }

    public string HostMobile { get; set; }    
}

public class MeetingsDBContext: DbContext
{
    public DbSet<Meeting> Meetings { get; set; }
}

public class ReinviteVisitorModel
{
    public Visitor Info;
    public Meeting NewMeeting;
    public List<Meeting> Meetings;
}

我在模型中有字段,例如Id,我期望数据库填充我将在动作CreateMeeting()中编写的内容。是否必须在表单中使用模型中的所有字段?

2 个答案:

答案 0 :(得分:5)

视图中的模型是ReinviteVisitorModel的类型,这意味着自发布ReinviteVisitorModel

以来POST方法的签名必须匹配
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult CreateMeeting(ReinviteVisitorModel model)

或者,您可以使用Prefix的{​​{1}}属性从您发布的表单控件的名称中删除BindAttribute前缀。

NewMeeting

附注:从隐藏输入中删除public ActionResult CreateMeeting([Bind(Prefix="NewMeeting")]Meeting model) ,然后在将模型传递给视图之前在GET方法中设置new { @Value = Model.Info.SubjectId }的值。

答案 1 :(得分:1)

您需要在ReinviteVisitorModel

中传递Action模型

使用此更新您的操作:

[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult CreateMeeting(ReinviteVisitorModel model)
{
    return RedirectToAction("ReinviteVisitor2", "Home", new { visitorId = model.NewMeeting.SubjectId });
}