将数据传递给控制器
Passing data to controller
我有一个 table 数据,每行显示商店的详细信息 - 可以有一个任意但唯一的 ID - 和一个商店编号 - 也是唯一的和非连续的 - 以及其他信息。
当我 select 一个商店编号时,我想显示商店的详细信息。
public class StoreViewModel
{
public FormatViewModel Formats { get; set; }
public List<StoreDetailsViewModel> StoreData { get; set; }
}
Formats 包含用于实现 MvcCheckBoxList 的数据,并在通过 FormMethod.GET.[=14 更新复选框时更新 StoreData =]
店铺型号:
public class StoreDetailsViewModel
{
public int StoreID { get; set; }
public int StoreNo { get; set; }
public string StoreFormat { get; set; }
public string Version { get; set; }
public int CellNo { get; set; }
[ForeignKey("CellNo")]
public virtual Cell Cell { get; set; }
}
索引 页面显示 table 个商店:
@using (Html.BeginForm("Details", "Store", FormMethod.Post))
{
<table class="table table-condensed table-bordered table-hover table-striped small">
<tr>
@*<th class="text-center"></th>*@
<th class="text-center">No.</th>
<th class="text-center">Format</th>
<th class="text-center">CBO Version</th>
<th class="text-center">Cell</th>
<th class="text-center"></th>
</tr>
@foreach (var item in Model.StoreData)
{
<tr>
<td class="text-center">
@Html.Hidden("List_StoreIDs", @item.StoreID)
@Html.Hidden("id", @item.StoreID)
@item.StoreNo
</td>
<td class="text-center">@Html.DisplayFor(modelItem => item.StoreFormat)</td>
<td class="text-center">@Html.DisplayFor(modelItem => item.Version)</td>
<td class="text-center">@Html.DisplayFor(modelItem => item.CellNo)</td>
<td>
<input type="submit" name="submit" value=@item.StoreNo />
</td>
</tr>
}
</table>
}
我无法开始工作的是传递商店列表中 selected 行的 StoreID(ActionLink 可以轻松工作,但我还需要传回 List_StoreIDs)。
我希望 @Html.Hidden("id") 可以工作,但这是 总是 1。
控制器
public ActionResult Details(int? id, int[] List_StoreIDs, string submit, string book)
{
if (id == null)
{
return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
}
Store store = db.Stores.Find(id);
if (store == null)
{
return HttpNotFound();
}
... do something
}
有什么建议吗?
问候
克雷格
如果您让控制器接受 StoreViewModel
然后使用 Html.HiddenFor
,数据将在控制器中可用。
我看到的问题是 table 的每一行都有一个隐藏字段和提交按钮,但所有行都采用相同的表单。因此传递给控制器的 id 将是表单中所有 id 的列表(大概以“1”开头),而不是您单击的特定 id。
每行都有一个提交按钮。您是要在单击其中一个提交按钮时提交整个表单,还是只对包含提交按钮的行中更改的详细信息感兴趣?
如果是后者,您可以每行有一个表单,那么每个表单的 "id" 值都是正确的(您可能会将表单放在一个元素中,并使用Bootstrap css)
我有一个 table 数据,每行显示商店的详细信息 - 可以有一个任意但唯一的 ID - 和一个商店编号 - 也是唯一的和非连续的 - 以及其他信息。
当我 select 一个商店编号时,我想显示商店的详细信息。
public class StoreViewModel
{
public FormatViewModel Formats { get; set; }
public List<StoreDetailsViewModel> StoreData { get; set; }
}
Formats 包含用于实现 MvcCheckBoxList 的数据,并在通过 FormMethod.GET.[=14 更新复选框时更新 StoreData =]
店铺型号:
public class StoreDetailsViewModel
{
public int StoreID { get; set; }
public int StoreNo { get; set; }
public string StoreFormat { get; set; }
public string Version { get; set; }
public int CellNo { get; set; }
[ForeignKey("CellNo")]
public virtual Cell Cell { get; set; }
}
索引 页面显示 table 个商店:
@using (Html.BeginForm("Details", "Store", FormMethod.Post))
{
<table class="table table-condensed table-bordered table-hover table-striped small">
<tr>
@*<th class="text-center"></th>*@
<th class="text-center">No.</th>
<th class="text-center">Format</th>
<th class="text-center">CBO Version</th>
<th class="text-center">Cell</th>
<th class="text-center"></th>
</tr>
@foreach (var item in Model.StoreData)
{
<tr>
<td class="text-center">
@Html.Hidden("List_StoreIDs", @item.StoreID)
@Html.Hidden("id", @item.StoreID)
@item.StoreNo
</td>
<td class="text-center">@Html.DisplayFor(modelItem => item.StoreFormat)</td>
<td class="text-center">@Html.DisplayFor(modelItem => item.Version)</td>
<td class="text-center">@Html.DisplayFor(modelItem => item.CellNo)</td>
<td>
<input type="submit" name="submit" value=@item.StoreNo />
</td>
</tr>
}
</table>
}
我无法开始工作的是传递商店列表中 selected 行的 StoreID(ActionLink 可以轻松工作,但我还需要传回 List_StoreIDs)。
我希望 @Html.Hidden("id") 可以工作,但这是 总是 1。
控制器
public ActionResult Details(int? id, int[] List_StoreIDs, string submit, string book)
{
if (id == null)
{
return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
}
Store store = db.Stores.Find(id);
if (store == null)
{
return HttpNotFound();
}
... do something
}
有什么建议吗?
问候 克雷格
如果您让控制器接受 StoreViewModel
然后使用 Html.HiddenFor
,数据将在控制器中可用。
我看到的问题是 table 的每一行都有一个隐藏字段和提交按钮,但所有行都采用相同的表单。因此传递给控制器的 id 将是表单中所有 id 的列表(大概以“1”开头),而不是您单击的特定 id。
每行都有一个提交按钮。您是要在单击其中一个提交按钮时提交整个表单,还是只对包含提交按钮的行中更改的详细信息感兴趣?
如果是后者,您可以每行有一个表单,那么每个表单的 "id" 值都是正确的(您可能会将表单放在一个元素中,并使用Bootstrap css)