Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/asp.net/33.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 使用Post提交时,ViewModel中的模型具有空属性值_C#_Asp.net_.net_Asp.net Mvc_Razor - Fatal编程技术网

C# 使用Post提交时,ViewModel中的模型具有空属性值

C# 使用Post提交时,ViewModel中的模型具有空属性值,c#,asp.net,.net,asp.net-mvc,razor,C#,Asp.net,.net,Asp.net Mvc,Razor,视图模型: public class EventViewModel { public Organization.Event Evt { get; set; } public GuestContributor Gst { get; set; } } 通过这个ViewModel,我使用Evt属性显示事件数据,该属性工作正常。和Gst属性,用于从同一页上的表单获取数据: @using (Html.BeginForm("GuestApplyToEvent", "Event", Form

视图模型:

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" />

                                           }}

问题是,即使在输入数据后,eventViewModel中的所有属性都为空。

您的Post方法需要GuestContributor类型的模型,但在您看来,您似乎正在使用模型eventViewModel,请尝试更改Post方法以接收模型类型eventViewModel

,这是我的第一个猜测。我试过了,但还是一样的问题。在这种情况下,evt和gst都有空参数。@AishwaryaShiva是所有的参数都为空,还是只是那些未在表单中绑定的参数?另外,您在表单中传递的属性是否具有公共级别和设置方法?@AishwaryaShiva我认为它现在正在工作?愉快的编码。您最后的评论让我想起,当我使用EventViewModel作为操作的输入进行测试时,我从未将值传递给字段。我的错!谢谢:@AishwaryaShiva没问题。很高兴我能帮你解释-1,好吗?
[HttpPost]
public ActionResult GuestApplyToEvent(Models.GuestContributor eventViewModel)
{
    if (!ModelState.IsValid)
        return View("~/Views/Event/Index.cshtml", eventViewModel);

    return View("~/Views/Event/Index.cshtml", eventViewModel);
}