[英]Bind Textbox to model in MVC4
In my MVC application, I have specified two conditions based on the Model.Count to display the values in View. 在我的MVC应用程序中,我已经基于Model.Count指定了两个条件来显示View中的值。
View 视图
@model IEnumerable<SampleECommerce.Models.DetailsModel>
@using (Html.BeginForm("Details", "Grid", new { UserID = Request.QueryString["UserID"], partnerid = Request.QueryString["Partnerid"] }, FormMethod.Post))
{
if (Model.Count() == 0)
{
@foreach (var item in Model)
{
<table>
<tr>
<td>
@Html.DisplayNameFor(model => model.FirstName)
<input id="FirstName" type="text" class="TextBoxBorder" name="FirstName" value="@item.FirstName" /> // When the Model count is zero, the label and textbox is not displayed.
</td>
</tr>
</table>
}
else
{
@foreach (var item in Model)
{
<table>
<tr>
<td>
@Html.DisplayNameFor(model => model.FirstName)
<input id="MFirstName" type="text" class="TextBoxBorder" name="FirstName" value="@item.FirstName" />
</td>
</tr>
</table>
}
Controller 调节器
public ActionResult Details()
{
string userid = Request.QueryString["UserID"];
string partnerid = Request.QueryString["Partnerid"];
con.Open();
SqlCommand cmd = new SqlCommand("select FirstName from Details where UserID = +userid+", con);
SqlDataReader dr = cmd.ExecuteReader();
List<DetailsModel> objmodel = new List<DetailsModel>();
while (dr.Read())
{
objmodel.Add(new DetailsModel()
{
FirstName = dr["First Name"].ToString(),
});
}
dr.Close();
return View(objmodel);
}
When the Model.Count is zero, the label and textbox are not displayed. 当Model.Count为零时,不显示标签和文本框。
I am trying to insert new value to textbox when the model.count is zero based on the userid 我想基于userid在model.count为零时向textbox插入新值
I tried to bind the textbox to model all the ways specified in the Link . 我试图绑定文本框以模拟链接中指定的所有方式。
1. @Html.TextBoxFor(model => model.FirstName) Error in FirstName stating "System.Collections.Generic.IEnumerable doesnot find definition for FirstName or no extension method " 1. @ Html.TextBoxFor(model => model.FirstName) FirstName中的错误说明“System.Collections.Generic.IEnumerable找不到FirstName的定义或没有扩展方法”
2. @Html.TextBox(model=>model.FirstName) "Error stating Cannot convert Lamba expression to string type" 2. @ Html.TextBox(model => model.FirstName) “错误说明无法将Lamba表达式转换为字符串类型”
How to bind and display the textbox value to the model when the model.count is zero. 当model.count为零时,如何将文本框值绑定并显示到模型。 Any suggestions ??
有什么建议么 ??
when the Model.Count
is 0, foreach
does nothing . 当
Model.Count
为0时, foreach
什么都不做。
@model IEnumerable<SampleECommerce.Models.DetailsModel>
@using (Html.BeginForm("Details", "Grid", new { UserID = Request.QueryString["UserID"], partnerid = Request.QueryString["Partnerid"] }, FormMethod.Post))
{
<table>
if (Model.Count() == 0)
{
<tr>
<td>
@Html.DisplayNameFor(model => model.FirstName)
<input id="FirstName" type="text" class="TextBoxBorder" name="FirstName" /> // When the Model count is zero, the label and textbox is not displayed.
</td>
</tr>
}
else
{
@foreach (var item in Model)
{
<tr>
<td>
@Html.DisplayNameFor(model => model.FirstName)
<input id="MFirstName" type="text" class="TextBoxBorder" name="FirstName" value="@item.FirstName" />
</td>
</tr>
}
}
<tr>
<td>
<input type="submit" value="submit" />
</td>
</tr>
</table>
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.