私の MVC アプリケーションでは、View に値を表示するために、Model.Count に基づいて 2 つの条件を指定しました。
意見
@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 がゼロの場合、ラベルとテキスト ボックスは表示されません。
ユーザーIDに基づいてmodel.countがゼロの場合、テキストボックスに新しい値を挿入しようとして
います。リンク
で指定されたすべての方法でテキストボックスをモデルにバインドしようとしました。
1. @Html.TextBoxFor(model => model.FirstName)
「System.Collections.Generic.IEnumerable で FirstName の定義が見つからないか、拡張メソッドがありません」という FirstName のエラー
2. @Html.TextBox(model=>model.FirstName)
「Lamba 式を文字列型に変換できないというエラー」
model.count がゼロの場合に、テキスト ボックスの値をモデルにバインドして表示する方法。助言がありますか ??