Asp.net mvc 4 如何将所选日期作为输入传递到MVC4中?
当我使用下面的代码时,我得到的是未设置为mvc4中对象实例的null引用。我使用FormCollection方法传递日期作为输入。当我跟踪时,它显示了sd的空值(sd是startdate变量)。如何解决此异常 控制器:Asp.net mvc 4 如何将所选日期作为输入传递到MVC4中?,asp.net-mvc-4,Asp.net Mvc 4,当我使用下面的代码时,我得到的是未设置为mvc4中对象实例的null引用。我使用FormCollection方法传递日期作为输入。当我跟踪时,它显示了sd的空值(sd是startdate变量)。如何解决此异常 控制器: [HttpPost] public ActionResult Index(FormCollection formCollection) { List<tblGroup> allg = new List<tblGroup&
[HttpPost]
public ActionResult Index(FormCollection formCollection)
{
List<tblGroup> allg = new List<tblGroup>();
using (portalconnectionstring con = new portalconnectionstring())
{
allg = con.grt.OrderBy(m => m.groupname).ToList();
}
ViewBag.disg = new SelectList(allg, "groupid", "groupname");
var sd = formCollection["txtDate"]; //Showing null value here while i traced out
var ed = formCollection["Txtenddate"];
....
.....
}
[HttpPost]
公共行动结果索引(FormCollection FormCollection)
{
List allg=新列表();
使用(portalconnectionstring con=new portalconnectionstring())
{
allg=con.grt.OrderBy(m=>m.groupname.ToList();
}
ViewBag.disg=新的选择列表(allg,“groupid”、“groupname”);
var sd=formCollection[“txtDate”];//在跟踪时在此处显示空值
var ed=formCollection[“Txtenddate”];
....
.....
}
视图:
@使用(Html.BeginForm(“Index”,“MyController”,FormMethod.Post,new{id=“form1”}))
{
@Label(“,new{id=“Label1”})
@Label(“组名”,新的{id=“lblGroupName”})
@Html.DropDownListFor(model=>model.groupid,@ViewBag.disg作为SelectList,“选择”)
@Html.ValidationMessageFor(model=>model.groupid)
}
您需要为输入提供一个名称
属性
<input id="txtDate" name="txtDate" type="text" value="Startdate: "/>
但是,如果您使用html帮助程序绑定到您的模型属性,例如@html.TextBoxFor(m=>m.txtDate)
,那么这将自动完成,在这种情况下,您可以使用POSt方法绑定回您的模型,而不是使用FormCollection
<input id="txtDate" name="txtDate" type="text" value="Startdate: "/>