【发布时间】:2019-09-08 14:38:01
【问题描述】:
我试图在局部视图中返回某些操作的结果。 因此,如果例如创建用户失败,部分视图应返回错误消息。 但不是返回部分视图,而是重定向到控制器/动作
网站:
@{
ViewBag.Title = "Rollen und Rechte";
}
@using (Ajax.BeginForm("Create", "Role", new AjaxOptions
{
InsertionMode = InsertionMode.Replace, //target element(#mydiv) will be replaced
UpdateTargetId = "mydiv"
}))
{
<table cellpadding="5">
<tr>
<td>Rollenname:</td>
<td><input type="text" name="Name" id="roleNameVal" placeholder="Neue Rolle" /></td>
</tr>
</table>
<br />
<label id="resultLabel"></label>
<input type="submit" value="Submit" id="btn_click" />
<div id="mydiv">
@{
Html.RenderPartial("_CreateResult");
}
</div>
}
控制器
[HttpPost]
public PartialViewResult Create(FormCollection val1)
{
try
{
// TODO: Add insert logic here
var value = val1.GetValue("name").AttemptedValue + "CREATED";
var res = new Result()
{
Success = true,
ResultValue = value
};
return PartialView("_CreateResult", res);
}
catch
{
return PartialView("bla");
}
}
局部视图
@model planemosIdUi.Dto.Result
@{
ViewBag.Title = "_CreateResult";
}
@{
if (Model?.Success == true)
{
<label>Erstellt</label>
}
else if(Model?.Success == false)
{
<label>Fehler</label>
}
else
{
<label>Do something</label>
}
}
所以发生的情况是部分视图返回但不在索引站点上,因为我期望它应该更改 div 的位置。 部分视图在重定向站点上返回
它从
重定向角色/角色索引/创建
但我只想更新 div
【问题讨论】:
标签: c# asp.net-mvc