ViewModel:
public class EventViewModel
{
public Organization.Event Evt { get; set; }
public GuestContributor Gst { get; set; }
}
通过此ViewModel,我正在使用Evt
属性来显示事件数据,该属性工作正常。 Gst属性用于从同一页面上的表单获取数据:
@using (Html.BeginForm("GuestApplyToEvent", "Event", FormMethod.Post))
{ <div class="row">
<div class="col-12 col-md-6">
@Html.TextBoxFor(c => c.Gst.Name, new { @class = "form-control", placeholder = "Name" })
@Html.ValidationMessageFor(c => c.Gst.Name)
</div>
<div class="col-12 col-md-6">
@Html.TextBoxFor(c => c.Gst.Email, new { @class = "form-control", placeholder = "Email Address" })
@Html.ValidationMessageFor(c => c.Gst.Email)
</div>
</div>
<div class="row">
<div class="col-12 ">
@Html.TextBoxFor(c => c.Gst.Mobile, new { @class = "form-control", placeholder = "Mobile (WhatsApp Number)" })
@Html.ValidationMessageFor(c => c.Gst.Mobile)
</div>
</div>
<input type="submit" class="btn dorne-btn" value="Apply" />
}}
将此ViewModel发送到我的视图时,我没有为Gst分配任何内容。
这是动作:
[HttpPost]
public ActionResult GuestApplyToEvent(Models.GuestContributor eventViewModel)
{
if (!ModelState.IsValid)
return View("~/Views/Event/Index.cshtml", eventViewModel);
return View("~/Views/Event/Index.cshtml", eventViewModel);
}
问题在于,即使输入了数据,eventViewModel
中的所有属性也为空。
答案 0 :(得分:1)
您的Post method
期望使用GuestContributor
类型的模型,但是在您的视图中,您似乎正在使用模型EventViewModel
,请尝试更改Post Method
来接收模型类型{{ 1}}