asp.net – 模型在表单发布到控制器时始终为NULL
发布时间:2020-05-23 21:27:31 所属栏目:asp.Net 来源:互联网
导读:每当我提交表单时,传递给控制器的模型都是NULL.我花了很长时间看这个.我想我在这里缺少一些基本的东西. @model VisitorPortal.Models.ReinviteVisitorModel@using (Html.BeginForm(CreateMeeting, Home, FormMethod.Post, new { @class = form-horizontal
|
每当我提交表单时,传递给控制器的模型都是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",new { visitorId = meeting.SubjectId });
}
我在模型中有字段,例如Id,我期望数据库填充我将在动作CreateMeeting()中编写的内容.是否必须在表单中使用模型中的所有字段? 解决方法您视图中的模型是typeof ReinviteVisitorModel,这意味着自发布ReinviteVisitorModel以来POST方法的签名必须匹配[HttpPost] [ValidateAntiForgeryToken] public ActionResult CreateMeeting(ReinviteVisitorModel model) 或者,您可以使用BindAttribute的Prefix属性从您要发布的表单控件的名称中删除NewMeeting前缀. public ActionResult CreateMeeting([Bind(Prefix="NewMeeting")]Meeting model) 附注:从隐藏输入中删除新的{@Value = Model.Info.SubjectId},然后在将模型传递给视图之前在GET方法中设置NewMeeting.SubjectId的值. (编辑:安卓应用网) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
相关内容
- UnobtrusiveJavaScriptEnabled键在.NET中的作用是什么?
- asp.net-mvc – ASP.NET Actionlink与glyphicon和文本与不同
- asp.net-mvc-4 – ASP.NET MVC电子商务解决方案(商业或开放
- asp.net-mvc – ASP.Net MVC – 视图中的编译器错误
- MVC 5远程验证
- asp.net-mvc – 从当前访问者获取CultureInfo并基于此设置资
- asp.net+js实现批量编码与解码的方法
- 什么是部署ASP.Net Web应用程序的好方法?
- 在Asp.net mvc5中使用用户名而不是电子邮件身份
- ASP.NET如何访问公共属性?
推荐文章
站长推荐
- Asp.net MVC – Jquery $.ajax错误回调没有返回r
- asp.net-mvc – EF实体与服务模型与查看模型(MVC
- asp.net-mvc-3 – 从ASP的Ajax.ActionLink获取JS
- asp.net-core – 在ASP.NET Core中将html导出为p
- ASP.NET – 在屏幕底部显示应用程序生成日期/信息
- asp.net – 如何使用外部登录提供程序创建刷新令
- iis – ServerManager构造函数在测试环境中崩溃
- asp.net-mvc – 如何在asp.net mvc 3中使用@html
- 如何在不使用角色的情况下使用ASP.NET WebAPI实现
- ASP.NET MVC中数据注释的默认资源
热点阅读
