将显示的 ViewModel 数据与文本框数据一起传递到控制器 asp.net mvc 3
ViewModel 类
public class CreateMRViewModel
{
public Decimal? Freight { get; set; }
public IList<OrderLine> OrderLines { get; set; }
}
public class OrderLine
{
public string LineNumber { get; set; }
public string VendorPartNumber { get; set; }
public long? Quantity { get; set; }
public string UOM { get; set; }
public decimal PricePerUom { get; set; }
public long? ReturnQuantity { get; set; }
public long? QuantityAvaiToReturn { get; set; }
}
我的视图
@model CreateMRViewModel
@using (Html.BeginForm()) {
<fieldset>
<table>
<tr>
<th>Line Number</th>
<th>Part Number</th>
<th>UOM</th>
<th>PricePerUom</th>
<th>Available Qty</th>
<th>Return Qty</th>
</tr>
@for (int i = 0; i < Model.OrderLines.Count; i++)
{
<tr>
<td>@Html.DisplayFor(modelItem => Model.OrderLines[i].LineNumber)</td>
<td>@Html.DisplayFor(modelItem => Model.OrderLines[i].VendorPartNumber)</td>
<td>@Html.DisplayFor(modelItem => Model.OrderLines[i].UOM)</td>
<td>@Html.DisplayFor(modelItem => Model.OrderLines[i].PricePerUom)</td>
<td>@Html.DisplayFor(modelItem => Model.OrderLines[i].Quantity)</td>
<td>@Html.TextBoxFor(modelItem => Model.OrderLines[i].ReturnQuantity)</td>
</tr>
}
</table>
<br />
<div class="display-label">@Html.LabelFor(model => Model.Freight)</div>
<div class="display-field">@Html.TextBox("Freight", new Int64())</div>
<br />
<br />
<input class="submitForm" type="submit" value="Create Return" />
</fieldset>
}
操作控制器
[HttpPost]
public ActionResult CreateMR(CreateMRViewModel mrViewModel)
{
//some code here
}
我的问题是:我将 CreateMRViewModel 获取到控制器,但只有文本框值可供我使用。如何获取显示的值(例如 LineNumber 或 VendorPartNumber)以在控制器方法中使用?
ViewModel class
public class CreateMRViewModel
{
public Decimal? Freight { get; set; }
public IList<OrderLine> OrderLines { get; set; }
}
public class OrderLine
{
public string LineNumber { get; set; }
public string VendorPartNumber { get; set; }
public long? Quantity { get; set; }
public string UOM { get; set; }
public decimal PricePerUom { get; set; }
public long? ReturnQuantity { get; set; }
public long? QuantityAvaiToReturn { get; set; }
}
My View
@model CreateMRViewModel
@using (Html.BeginForm()) {
<fieldset>
<table>
<tr>
<th>Line Number</th>
<th>Part Number</th>
<th>UOM</th>
<th>PricePerUom</th>
<th>Available Qty</th>
<th>Return Qty</th>
</tr>
@for (int i = 0; i < Model.OrderLines.Count; i++)
{
<tr>
<td>@Html.DisplayFor(modelItem => Model.OrderLines[i].LineNumber)</td>
<td>@Html.DisplayFor(modelItem => Model.OrderLines[i].VendorPartNumber)</td>
<td>@Html.DisplayFor(modelItem => Model.OrderLines[i].UOM)</td>
<td>@Html.DisplayFor(modelItem => Model.OrderLines[i].PricePerUom)</td>
<td>@Html.DisplayFor(modelItem => Model.OrderLines[i].Quantity)</td>
<td>@Html.TextBoxFor(modelItem => Model.OrderLines[i].ReturnQuantity)</td>
</tr>
}
</table>
<br />
<div class="display-label">@Html.LabelFor(model => Model.Freight)</div>
<div class="display-field">@Html.TextBox("Freight", new Int64())</div>
<br />
<br />
<input class="submitForm" type="submit" value="Create Return" />
</fieldset>
}
Action Controller
[HttpPost]
public ActionResult CreateMR(CreateMRViewModel mrViewModel)
{
//some code here
}
My problem is: I get the CreateMRViewModel to controller but only textbox values are available for me to use. How can I get the displayed values such as LineNumber or VendorPartNumber for use in controller method?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您实际上并没有发布这些
Properties
的任何值,因为您正在使用DisplayFor()
如果您只想将其传递给
[HttpPost]
方法,您将需要用隐藏输入来表示它或使其可编辑。You're not actually posting any values for those
Properties
since you're usingDisplayFor()
If you simply want to just pass it through to the
[HttpPost]
method you're going to need to represent it with a hidden input or make it editable.