Asp.net core 在父对象的提交操作中获取局部视图模型
我可以像这样动态地将多个partialview添加到我的页面中 Create.cshtmlAsp.net core 在父对象的提交操作中获取局部视图模型,asp.net-core,asp.net-mvc-partialview,Asp.net Core,Asp.net Mvc Partialview,我可以像这样动态地将多个partialview添加到我的页面中 Create.cshtml @model Opto.Models.GlassOrder ... ... ... <div class="text-center" dir="rtl" id="ttt"> </div> <a id="add1" style="cursor:pointer&quo
@model Opto.Models.GlassOrder
...
...
...
<div class="text-center" dir="rtl" id="ttt">
</div>
<a id="add1" style="cursor:pointer">add</a>
<script>
var rowNum = 0;
$('#add1').click(function () {
rowNum++;
$.get('/Glasses/DisplayBill?id=' + rowNum, function (partial) {
console.log(partial);
$('#ttt').append(partial);
});
});
</script>
@model Opto.Models.BillFarSighted
<div style="display: inline-block">
<div class="row form-group">
<label asp-for="PackFactor" class="col-5 text-left col-form-label"></label>
<div class="col-7">
<select asp-for="PackFactor" class="form-control" asp-items="Html.GetEnumSelectList<Compression>()">
<option selected value="">انتخاب کنید</option>
</select>
<span asp-validation-for="PackFactor" class="text-danger"></span>
</div>
</div>
...
...
...
</div>
GlassesController.cs
public partial class BillFarSighted
{
public long Id { get; set; }
public long RecipeId { get; set; }
...
...
...
}
public ActionResult DisplayBill(int id)
{
BillFarSighted billFarSighted = new BillFarSighted() { PackFactor = 3 };
return PartialView("BillFarSighted", billFarSighted);
}
[HttpPost]
public async Task<IActionResult> Create(List<BillFarSighted> billFarSighteds)
{
....
}
public ActionResult DisplayBill(int-id)
{
BillForesight BillForesight=新BillForesight(){PackFactor=3};
返回PartialView(“有远见的”,有远见的);
}
[HttpPost]
公共异步任务创建(列表有远见者)
{
....
}
但是,当我提交父窗体(在创建操作中)时,
BillForVisions
list为空,如何在controller中获取这些部分模型?list对象绑定的关键是确保将方括号中的顺序索引添加到窗体字段的name属性中,例如[0].PackFactor
在您的情况下,可以将rowNum作为索引
Create.csthml
<form asp-action="Create" method="post">
<div class="text-center" dir="rtl" id="ttt">
</div>
<input type="submit" value="submit" class="btn btn-primary" />
</form>
<a id="add1" style="cursor:pointer">add</a>
@section scripts{
<script>
var rowNum = 0;
$('#add1').click(function () {
$.get('/Glasses/DisplayBill?id=' + rowNum, function (partial) {
console.log(partial);
$('#ttt').append(partial);
rowNum++;
});
});
</script>
}
控制器:
public ActionResult DisplayBill(int id)
{
BillFarSighted billFarSighted = new BillFarSighted() { Id = id };
return PartialView("BillFarSighted", billFarSighted);
}
[HttpPost]
public async Task<IActionResult> Create(List<BillFarSighted> billFarSighteds)
{
//some codes
}
public ActionResult DisplayBill(int-id)
{
Bill远视Bill远视=新Bill远视(){Id=Id};
返回PartialView(“有远见的”,有远见的);
}
[HttpPost]
公共异步任务创建(列表有远见者)
{
//一些代码
}
结果:
你能给我们看一下partialview和Bill远见卓识的模型吗?压缩是什么?我不能发送我问题中的所有细节,因为stack说它看起来太多代码了,压缩是一个枚举,不重要,我的问题是在提交操作中它不会得到部分模型,我确信部分模型没有问题,这只是因为父母的努力,我不知道如何感谢你,你救了我
public class BillFarSighted
{
public long Id { get; set; }
public long RecipeId { get; set; }
public long PackFactor { get; set; }
}
public enum Compression
{
AAA = 1,
BBB = 2,
CCC = 3,
DDD = 4
}
public ActionResult DisplayBill(int id)
{
BillFarSighted billFarSighted = new BillFarSighted() { Id = id };
return PartialView("BillFarSighted", billFarSighted);
}
[HttpPost]
public async Task<IActionResult> Create(List<BillFarSighted> billFarSighteds)
{
//some codes
}