简体   繁体   English

从网址动态重定向

[英]Dynamic Redirect from Url.Action

I'm building an object using values passed to a Controller through a Url.Action that occurs when the user clicks a button. 我正在使用通过用户单击按钮时发生的Url.Action传递给Controller的值来构建对象。

$.post("@Url.Action("CreateObj", "ObjectController")", {
    value: $value
})

In the the method that it calls, I create an object, assign it an id and save the object to the database. 在调用的方法中,我创建一个对象,为其分配一个ID,然后将该对象保存到数据库中。

public void CreateObj(string value)
{
    Object newObj = Object(value);

    //Magic saving newObj to database

    int id = newObj.objectId; //Saves the object's id to a variable for demo purposes
}

Now I want to immediately redirect to the Edit page for this new object where the URL is "./Object/Edit/" + newObj.objectId. 现在,我想立即重定向到该新对象的“编辑”页面,其中URL为“ ./Object/Edit/”+ newObj.objectId。

I've tried redirecting within the C# CreateObj method, I've tried passing the objectId through the ViewData so I can redirect in a callback function but I can't find a way to get either to work. 我尝试在C#CreateObj方法中进行重定向,尝试通过ViewData传递objectId,以便可以在回调函数中进行重定向,但找不到任何一种方法可以正常工作。

How can I bring the user to the Edit page after creating an Object if I don't know the ID before it is created, and can't access it from the view? 如果创建对象之前我不知道ID,并且无法从视图访问它,该如何在创建对象后将用户带到“编辑”页面? Am I missing something simple? 我是否缺少简单的东西?

您可以在创建对象的函数中使用回调,一旦创建了对象,便会触发该回调,将对象ID传递给回调,然后在回调内部将用户重定向到新页面。

Assuming your edit action method looks like this 假设您的编辑操作方法如下所示

public ActionResult Edit(string value){
   //your magic codes
}

you can call this like normal function calls. 您可以像正常函数调用一样调用它。

public void CreateObj(string value)
{
   Object newObj = Object(value);

   //Magic saving newObj to database

   int id = newObj.objectId; //Saves the object's id to a variable for demo purposes
   Edit(id);   // <-- Normal function call
}

This will automatically redirect to the edit page. 这将自动重定向到编辑页面。

Since you're doing an ajax request, your Action should return the Id of the created object, and you should handle the redirection via JS. 由于您正在执行ajax请求,因此您的Action应该返回所创建对象的ID,并且您应该通过JS处理重定向。 So change the return type and value of your Action to: 因此,将Action的返回类型和值更改为:

public ActionResult CreateObj(string value) {
    int id = newObj.objectId;
    return Json(id, JsonRequestBehavior.AllowGet);
}

And register a callback function on your post() method to perform the redirection: 并在您的post()方法上注册一个回调函数以执行重定向:

 $.post("@Url.Action("CreateObj", "Object")", {
                value:"value"
            }).done(function (data) { // data represents the returned id
                //handle success
                window.location.href = `@Url.Action("Edit", "Object")/${data}`;
            }).fail(function () {
                   //Handle failure
            })

• Check how $.post() works on jQuery.post() . •检查$ .post()在jQuery.post()上如何工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM