【问题标题】:How to return partial view without site redirection如何在没有站点重定向的情况下返回部分视图
【发布时间】: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


    【解决方案1】:

    【讨论】:

    • 不要只用链接回答问题,因为它们可能会过期。贴一些例子
    猜你喜欢
    • 2018-11-03
    • 1970-01-01
    • 2020-02-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-09-28
    • 2014-09-08
    • 1970-01-01
    相关资源
    最近更新 更多