mvc4 中的模型值为空
Model Value is null in mvc4
我正在使用 post 方法,我正在尝试 post 文本框的值到数据库,为此我正在执行所有必要的步骤,但在那 post 方法我的模型是空的。找到下面的代码,
我的简单控制器
[HttpPost]
public ActionResult Index(QuestionBankModel question)
{
return View();
}
我的模型
public class QuestionBankModel
{
public string question { get; set; }
}
我的观点
@model OnlinePariksha.Models.QuestionBankModel
@{
var CustomerInfo = (OnlinePariksha.Models.UserLoginModel)Session["UserInfo"];
}
@{
ViewBag.Title = "Index";
}
@{
Layout = "~/Views/Shared/Admin.cshtml";
}
@using (Html.BeginForm("Index", "AdminDashboard", FormMethod.Post))
{
<div id="questionsDiv" style="width:100%; display:none;">
<div style="width:200px">
<table style="width:100%">
<tr>
<td><span><b>Question:</b></span></td>
<td>
@Html.TextBox(Model.question, new Dictionary<string, object> { { "class", "textboxUploadField" } })
</td>
</tr>
</table>
</div>
<div class="clear"></div>
<div>
<input type="submit" class="sucessBtn1" value="Save" />
</div>
</div>
}
我错过了什么吗?
您尝试过使用@HTML.TextBoxFor吗?
@Html.TextBoxFor(m=>m.question,new Dictionary<string, object> { { "class", "textboxUploadField" } })
Html.TextBox 使用不正确,因为第一个参数是文本框的名称,而您传递的是问题的值。我会改用这个:
@Html.TextBoxFor(m => m.question)
您的问题是 POST 方法参数名称与您的模型 属性 同名(因此模型绑定失败)。将方法签名更改为
public ActionResult Index(QuestionBankModel model)
{
...
}
或与模型不同的任何其他参数名称 属性。
作为解释,DefaultModelBinder
首先初始化 QuestionBankModel
的一个新实例。然后它检查表单(和其他)值并看到 question="SomeStringYouEntered"
。然后搜索名为 question
的 属性(以便设置其值)。它找到的第一个是您的方法参数,因此它在内部执行 QuestionBankModel question = "SomeStringYouEntered";
失败(您不能将字符串分配给复杂对象)并且模型参数现在变为 null
.
我正在使用 post 方法,我正在尝试 post 文本框的值到数据库,为此我正在执行所有必要的步骤,但在那 post 方法我的模型是空的。找到下面的代码, 我的简单控制器
[HttpPost]
public ActionResult Index(QuestionBankModel question)
{
return View();
}
我的模型
public class QuestionBankModel
{
public string question { get; set; }
}
我的观点
@model OnlinePariksha.Models.QuestionBankModel
@{
var CustomerInfo = (OnlinePariksha.Models.UserLoginModel)Session["UserInfo"];
}
@{
ViewBag.Title = "Index";
}
@{
Layout = "~/Views/Shared/Admin.cshtml";
}
@using (Html.BeginForm("Index", "AdminDashboard", FormMethod.Post))
{
<div id="questionsDiv" style="width:100%; display:none;">
<div style="width:200px">
<table style="width:100%">
<tr>
<td><span><b>Question:</b></span></td>
<td>
@Html.TextBox(Model.question, new Dictionary<string, object> { { "class", "textboxUploadField" } })
</td>
</tr>
</table>
</div>
<div class="clear"></div>
<div>
<input type="submit" class="sucessBtn1" value="Save" />
</div>
</div>
}
我错过了什么吗?
您尝试过使用@HTML.TextBoxFor吗?
@Html.TextBoxFor(m=>m.question,new Dictionary<string, object> { { "class", "textboxUploadField" } })
Html.TextBox 使用不正确,因为第一个参数是文本框的名称,而您传递的是问题的值。我会改用这个:
@Html.TextBoxFor(m => m.question)
您的问题是 POST 方法参数名称与您的模型 属性 同名(因此模型绑定失败)。将方法签名更改为
public ActionResult Index(QuestionBankModel model)
{
...
}
或与模型不同的任何其他参数名称 属性。
作为解释,DefaultModelBinder
首先初始化 QuestionBankModel
的一个新实例。然后它检查表单(和其他)值并看到 question="SomeStringYouEntered"
。然后搜索名为 question
的 属性(以便设置其值)。它找到的第一个是您的方法参数,因此它在内部执行 QuestionBankModel question = "SomeStringYouEntered";
失败(您不能将字符串分配给复杂对象)并且模型参数现在变为 null
.