asp.net - 模型在发送到 Controller 的表单上始终为 NULL

标签 asp.net asp.net-mvc

每当我提交表单时,传递到 Controller 的模型都是 NULL。我花了很长时间看这个。我想我在这里遗漏了一些基本的东西。

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

模型是:

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

Controller 操作是:

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

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

最佳答案

您 View 中的模型是 typeof ReinviteVisitorModel,这意味着自您发布 ReinviteVisitorModel 以来,POST 方法的签名必须匹配

[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult CreateMeeting(ReinviteVisitorModel model)

或者,您可以使用 BindAttributePrefix 属性从您要发布的表单控件的名称中去除 NewMeeting 前缀。

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

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

关于asp.net - 模型在发送到 Controller 的表单上始终为 NULL,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38321628/

相关文章:

asp.net - 为什么 ASP.NET MVC 中的移动 View 在不同的服务器上显示不同?

ASP.NET MVC5 : Unit Testing a controller with a session

asp.net - 可以在 Webforms 项目中托管 ASP.NET MVC Controller + View ...?

asp.net-mvc - 在 ASP.NET MVC3 中使用 Razor 的文件上传控件

javascript - 在c#中检索html td onClick事件值

.net - 设计一个 asp :button server control

c# - 如何使用 iTextSharp 保留 CSS?

javascript - 将隐藏字段传递给 JavaScript 函数

javascript - ajaxToolkit 中的 PopupControlExtender 不适用于启用 tinymce 的文本框

asp.net-mvc - ASP.NET MVC UpdateModel 抛出异常 : "Model could not be updated"