您也许可以通过使用纯 html 来做到这一点,但是,我不确定这是否可行,您可能需要编辑输入的名称属性,例如Name="Model.CustomerName" 而不是 Name="CustomerName"。
@Html.BeginForm("actionName", "controllerName", FormMethod.Post, new { @class = "form" })
{
<div class="form-group">
<label class="control-label" for="CustomerName">Name</label>
<input class="form-control" name="CustomerName" id="CustomerName">
</div>
<div class="form-group">
<label class="control-label" for="CustomerPhone">Phone</label>
<input class="form-control" name="CustomerPhone" id="CustomerPhone">
</div>
<div class="form-group">
<label class="control-label" for="Message">Message</label>
<textarea class="form-control" name="Message" id="Message"></textarea>
</div>
<div class="form-group">
<input class="btn btn-success" type="submit">Send</input >
</div>
}
不过,另一种方法是创建一个包含两个模型的视图模型。
第 1 步。创建一个列出这两个类的新模型。
public class ProductsInfoVM
{
public List<Product> Products { get; set; }
public List<InfoRequest> InfoRequests { get; set; }
}
第 2 步。创建视图:
<table class="table">
<tr>
<th>
@Html.DisplayNameFor(model => model.Products[0].InfoRequest.CustomerName)
</th>
<th>
@Html.DisplayNameFor(model => model.Products[0].Description)
</th>
<th>
@Html.DisplayNameFor(model => model.Products[0].IsAvailable)
</th>
<th></th>
</tr>
@foreach (var item in Model.Products)
{
<tr>
<td>
@Html.DisplayFor(modelItem => item.InfoRequest.CustomerName)
</td>
<td>
@Html.DisplayFor(modelItem => item.Description)
</td>
<td>
@Html.DisplayFor(modelItem => item.IsAvailable)
</td>
<td>
@Html.ActionLink("Edit", "Edit", new { id = item.ProductId }) |
@Html.ActionLink("Details", "Details", new { id = item.ProductId }) |
@Html.ActionLink("Delete", "Delete", new { id = item.ProductId })
</td>
</tr>
}
</table>
表格:
@Html.BeginForm("actionName", "controllerName", FormMethod.Post, new { @class = "form" })
{
<div class="form-group">
@Html.LabelFor(x => x.InfoRequests[0].CustomerName, new { @class="control-label" })
@Html.TextBoxFor(x => x.InfoRequests[0].CustomerName, new { @class = "form-control" })
@Html.ValidationMessageFor(x => x.InfoRequests[0].CustomerName, "Your name cannot be blank" ,new { @class = "form-control" })
</div>
<div class="form-group">
@Html.LabelFor(x => x.InfoRequests[0].CustomerPhone, new { @class = "control-label" })
@Html.TextBoxFor(x => x.InfoRequests[0].CustomerPhone, new { @class = "form-control" })
@Html.ValidationMessageFor(x => x.InfoRequests[0].CustomerPhone, "Your phone cannot be blank", new { @class = "form-control" })
</div>
<div class="form-group">
@Html.LabelFor(x => x.InfoRequests[0].Message, new { @class = "control-label" })
@Html.TextBoxFor(x => x.InfoRequests[0].Message, new { @class = "form-control" })
@Html.ValidationMessageFor(x => x.InfoRequests[0].Message, "Your message cannot be blank", new { @class = "form-control" })
</div>
<div class="form-group">
<input class="btn btn-success" type="submit">Send</input >
</div>
}