在控制器的POST操作中没有显示ViewBag值?

Ana*_*tic 0 asp.net-mvc razor viewbag asp.net-mvc-5

我在我的MVC5应用程序(Oracle后端)中有一种情况,其中我必须在转移到视图的GET时存储当前URL(此URL包含用户以前的导航位置以及在此处列出的任何/所有排序/过滤条件数据网格).

起初我以为我已经通过使用Session变量来解决这个问题Session["returnURL"],但是当它在我的localhost上运行时,许多尝试仍然导致我的会话变量NullReferenceException在我的Production Server上执行期间抛出.我现在在GET Edit操作上有以下内容:

        if (Request.UrlReferrer.AbsoluteUri != null)
        {
            ViewBag.ReturnURL = Request.UrlReferrer.AbsoluteUri;
        }
        else
        {
            ViewBag.ReturnURL = null;
        }
Run Code Online (Sandbox Code Playgroud)

如果上面的值不为null,我将它存储在ViewBag中以访问我的View(将其指定为hrefon actionlink):

         @{
                if (ViewBag.ReturnURL == null)
                {
                    <span class="btn btn-default">
                        @Html.ActionLink("Back to List", "Index", "Home", new { @class = "btn btn-default" })
                    </span>
                }
                else
                {
                    <a href="@ViewBag.ReturnURL"><span class="btn btn-default">Back to List</span></a>
                }
            }
                     |
                    <input type="submit" value="Save"  class="btn btn-primary" />
Run Code Online (Sandbox Code Playgroud)

我现在的问题是,当我尝试将更改保存到我的记录(从而进入我的编辑操作的POST)时,我收到:

Server Error in '/' Application. Value cannot be null or empty. Parameter name: url. Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code. Exception Details: System.ArgumentException: Value cannot be null or empty. Parameter name: url

以下是我的POST编辑操作的相关代码:

        if (ModelState.IsValid)
        {
            iNV_Assets.MODIFIED_DATE = DateTime.Now;
            iNV_Assets.MODIFIED_BY = System.Environment.UserName;

            db.Entry(my_Model).State = EntityState.Modified;
            await db.SaveChangesAsync();

            var returnURL = ViewBag.ReturnURL;
            return Redirect(returnURL);
        }
Run Code Online (Sandbox Code Playgroud)

记录随着我所做的任何更改而保存,但是当尝试使用用户的先前URL(以及所有指定的搜索条件)重定向回主视图时,会抛出错误.

有谁知道为什么我ViewBag.ReturnURLNULL在POST编辑动作结果中出现?关于如何用替代解决方案实现我的目标的任何想法也许?

use*_*147 6

您想要回发的任何值都需要位于表单标记内的输入字段中,因此请将其放在表单中的某个位置

<input type="hidden" value="@ViewBag.ReturnURL" name="returnURL" />
Run Code Online (Sandbox Code Playgroud)

那么你需要为你的动作添加一个参数string returnUrl或使用Request.Form.其他选项包括向模型添加属性并设置它并为该属性添加隐藏字段

我经常使用它.将您的视图更改为

@if (ViewBag.ReturnURL == null)
{
    <span class="btn btn-default">
        @Html.ActionLink("Back to List", "Index", "Home", new { @class = "btn btn-default" })
    </span>
}
else
{
    <a href="@ViewBag.ReturnURL"><span class="btn btn-default">Back to List</span></a>
    <input type="hidden" value="@ViewBag.ReturnURL" id="returnURL" />
}
Run Code Online (Sandbox Code Playgroud)

然后将您的编辑操作更改为

[HttpPost]
public ActionResult Edit(ModelName my_Model, string returnURL)
{
    if (ModelState.IsValid)
    {
        iNV_Assets.MODIFIED_DATE = DateTime.Now;
        iNV_Assets.MODIFIED_BY = System.Environment.UserName;

        db.Entry(my_Model).State = EntityState.Modified;
        await db.SaveChangesAsync();

        if (!string.IsNullOrEmpty(returnURL))
            return Redirect(returnURL);
    }
    return View(my_Model);
}
Run Code Online (Sandbox Code Playgroud)