0
在我的MVC應用程序中,我已經根據Model.Count指定了兩個條件以在View中顯示值。
查看將文本框綁定到MVC4中的模型
@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>
}
控制器
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);
}
當Model.Count是零,不顯示在標籤和文本框。
我試圖插入新的價值的文本框時,基於用戶標識
我試圖將文本框在Link規定的所有方法建模結合model.count爲零。
1. @ Html.TextBoxFor(型號=> model.FirstName) 錯誤姓陳述 「System.Collections.Generic.IEnumerable不列入找到姓定義或無擴展方法」
2. @ Html.TextBox (model => model.FirstName) 「錯誤說明無法將Lamba表達式轉換爲字符串類型」
如何在model.count爲零時將文本框值綁定並顯示到模型。 有什麼建議?
莫非我能綁定文本框模型值,當Model.Count爲零? – kk1076 2013-05-03 11:43:58
當文本框爲空時,我需要基於用戶標識符從文本框中插入新值 – kk1076 2013-05-03 11:52:03
當頁面加載時,首先我顯示一個空的文本框。如果基於用戶標識存在模型值,則該值將顯示在文本框中。否則從文本框中插入新值並更新它。 – kk1076 2013-05-03 12:36:41