Asp.net 在向控制器发送表单时,模型始终为空

Asp.net 在向控制器发送表单时,模型始终为空,asp.net,asp.net-mvc,Asp.net,Asp.net Mvc,每当我提交表单时,传递给控制器的模型都是空的。我花了很长时间看这个。我想我遗漏了一些基本的东西 @model VisitorPortal.Models.ReinviteVisitorModel @using (Html.BeginForm("CreateMeeting", "Home", FormMethod.Post, new { @class = "form-horizontal", role = "form" })) { @Html.AntiForgeryToken() &

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

@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>
}

我在模型中有Id等字段,我希望数据库填充这些字段,我将在action CreateMeeting()中写入这些字段。模型中的所有字段都必须在表单中使用吗?

您视图中的模型类型为
reinvititormodel
,这意味着POST方法的签名必须匹配,因为您的POST
reinvititormodel

[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult CreateMeeting(ReinviteVisitorModel model)
或者,您可以使用
BindAttribute
Prefix
属性,从正在发布的表单控件的名称中删除
NewMeeting
前缀

public ActionResult CreateMeeting([Bind(Prefix="NewMeeting")]Meeting model)

旁注:从隐藏的输入中删除
new{@Value=Model.Info.SubjectId}
,而是在将模型传递到视图之前,在GET方法中设置
NewMeeting.SubjectId
的值。

您需要在
操作中传递
reinvititormodel

使用以下内容更新您的操作:

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

您正在重定向到另一个操作
ReinviteVisitor2
,请添加其代码。顺便问一下,视图的文件名是什么?您传递的模型和您使用的模型是不同的,这就是为什么它返回null。好了,第一部分开始工作了。但是,如果我像您所描述的那样移动隐藏字段,当传递给CreateMeeting操作时,它将为null(Meeting.SubjectId)。我认为表单创建了一个新的会议对象?我没有说要移动隐藏的输入。只需使用
@Html.HiddenFor(m=>m.NewMeeting.SubjectId)
,但在GET方法中设置其值,以便获得真正的双向模型绑定,例如
NewMeeting.SubjectId=Info.SubjectId
(通过设置
属性来覆盖
HtmlHelper
方法的功能是一种不好的做法)Ok,这是有意义的。非常感谢。
public ActionResult CreateMeeting([Bind(Prefix="NewMeeting")]Meeting model)
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult CreateMeeting(ReinviteVisitorModel model)
{
    return RedirectToAction("ReinviteVisitor2", "Home", new { visitorId = model.NewMeeting.SubjectId });
}