我正在使用C#和.NET Framework 4.5.1开发ASP.NET MVC 5 Web.
我form在一个cshtml文件中有这个:
@model MyProduct.Web.API.Models.ConnectBatchProductViewModel
@{
Layout = null;
}
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width" />
<title>Create</title>
</head>
<body>
@if (@Model != null)
{
<h4>Producto: @Model.Product.ProductCode, Cantidad: @Model.ExternalCodesForThisProduct</h4>
using (Html.BeginForm("Save", "ConnectBatchProduct", FormMethod.Post))
{
@Html.HiddenFor(model => model.Product.Id, new { @id = "productId", @Name = "productId" });
<div>
<table id ="batchTable" class="order-list">
<thead>
<tr>
<td>Cantidad</td>
<td>Lote</td>
</tr>
</thead>
<tbody>
<tr>
<td>@Html.TextBox("ConnectBatchProductViewModel.BatchProducts[0].Quantity")</td>
<td>@Html.TextBox("ConnectBatchProductViewModel.BatchProducts[0].BatchName")</td>
<td><a class="deleteRow"></a></td>
</tr>
</tbody>
<tfoot>
<tr>
<td colspan="5" style="text-align: left;"> …Run Code Online (Sandbox Code Playgroud) 我在视图中添加了一个按钮.单击此按钮时,将添加部分视图.在我的表单中,我可以添加尽可能多的局部视图.提交此表单数据时,我无法将所有部分视图数据发送到控制器.我创建了一个具有所有属性的不同模型,并且我已经将该模型的列表添加到我的主模型中.任何人都可以给我一些技巧,以便我可以将所有部分视图内容发送到我的控制器?
在我看来
<div id="CSQGroup">
</div>
<div>
<input type="button" value="Add Field" id="addField" onclick="addFieldss()" />
</div>
function addFieldss()
{
$.ajax({
url: '@Url.Content("~/AdminProduct/GetColorSizeQty")',
type: 'GET',
success:function(result) {
var newDiv = $(document.createElement("div")).attr("id", 'CSQ' + myCounter);
newDiv.html(result);
newDiv.appendTo("#CSQGroup");
myCounter++;
},
error: function(result) {
alert("Failure");
}
});
}
Run Code Online (Sandbox Code Playgroud)
在我的控制器中
public ActionResult GetColorSizeQty()
{
var data = new AdminProductDetailModel();
data.colorList = commonCore.getallTypeofList("color");
data.sizeList = commonCore.getallTypeofList("size");
return PartialView(data);
}
[HttpPost]
public ActionResult AddDetail(AdminProductDetailModel model)
{
....
}
Run Code Online (Sandbox Code Playgroud)
在我的部分视图中
@model IKLE.Model.ProductModel.AdminProductDetailModel
<div class="editor-field">
@Html.LabelFor(model => model.fkConfigChoiceCategorySizeId)
@Html.DropDownListFor(model => model.fkConfigChoiceCategorySizeId, …Run Code Online (Sandbox Code Playgroud) 我在下面对这个问题进行了修改,这仍然是相同的,但希望通过模型以及我想要实现的目标以及我遇到问题的地方更加清晰.
下面显示了两个类,Company和Employee,Company有一个Employees列表.
这将是一个输入表单,因此开始时将没有数据.
最终,我希望用户能够根据需要向Company对象模型添加尽可能多的Employee对象,并更新Employee对象
我使用BeginCollectionItem在正确的轨道上,所以我可以添加/删除任意数量的Employee对象吗?当我单击Add按钮时,它会将其带到另一个页面上的部分视图(使用AjaxActionLink),但不能使用JavaScript.
更新 删除了AjaxActionLink并改为使用JavaScript.
指数
@model MvcTest.Models.Company
@{
ViewBag.Title = "Index";
Layout = "~/Views/Shared/_Layout.cshtml";
}
<h2>Company</h2>
<div>
@Html.LabelFor(m => m.Name)
@Html.EditorFor(m => m.Name)
</div>
<fieldset>
<legend>Employees</legend>
<div id="new-Employee">
@foreach (var Employee in Model.Employees)
{
Html.RenderPartial("_Employee", Employee);
}
</div>
<div>
<input type="button" id="addemployee" name="addemployee" value="Add Employee"/>
<br/>
</div>
<br/>
@section Scripts
{
<script type="text/javascript">
$('#addemployee').on('click', function () {
$.ajax({
async: false,
url: '/Company/AddNewEmployee'
}).success(function (partialView) {
$('#new-Employee').append(partialView);
});
});
</script>
}
</fieldset>
<div>
<input type="submit" value="Submit" />
</div> …Run Code Online (Sandbox Code Playgroud)