【发布时间】:2023-03-16 19:55:01
【问题描述】:
我有一个返回我需要的部分视图的部分视图,但我需要捕获模型,因为 json.encode 获取了与主视图一起发送的主要模型,因为没有提交到表单。
我需要使用部分视图对发送的模型进行建模,以便我可以使用它而不是 json.encode
public ActionResult addField(List<Destination> model)
{
model.Add(new Destination
{
path = String.Empty,
});
return PartialView("_ChampDestination",model);
}
这里是ajax调用
function addField(event) {
event.preventDefault();
var model = @Html.Raw(Json.Encode(Model.RepertoireDestinationMultiple));
$.ajax({
type: 'POST',
contentType: 'application/json',
url: '@Url.Action("addField", "Flux")',
data: JSON.stringify(model),
success: function (response) {
debugger;
var x = response.model;
$(".destinationMultiple").html(response);
}
, error: function(xhr, textStatus, error){
console.log(xhr.statusText);
console.log(textStatus);
console.log(error);}
});
}
我添加的新动作没有在主模型中注册。
【问题讨论】:
-
是读取表单并将其传递给操作的问题吗?据我所知,其他一切似乎都很好。为了清楚起见,也许包括一些示例
HTML和JSON? -
局部视图渲染得很好,我想要的是捕获随它发送的模型,这样如果我想添加其他字段我可以使用它,因为在第一次添加后有一个模型发送到查看有 2 个字段,我想使用那个
-
你已经在
var model中捕获了它,我倾向于将json存储在隐藏字段或data属性字段中,以便我以后可以读取/修改它。这就是你所追求的吗? -
是的,我想用用于创建局部视图 @Steve0 的数据填充该变量
-
顺便说一句,var 模型什么都没有,只是在尝试它是否有效。 @史蒂夫0
标签: jquery asp.net ajax model-view-controller