简体   繁体   English

jQuery 发布到控制器并重定向到 ASP.NET MVC 视图

[英]jQuery post to controller and redirect to ASP.NET MVC view

I have some checkboxes and a button (not in a form).我有一些复选框和一个按钮(不是在表单中)。

When the button is clicked, I have some jQuery and I am creating a post model which contains the values of the checked boxes and posting to a controller.单击按钮时,我有一些 jQuery,我正在创建一个包含选中框的值并发布到控制器的发布模型。

The controller then creates view models and I want to redirect the user to the correct view, passing the view model in to the view.然后控制器创建视图模型,我想将用户重定向到正确的视图,将视图模型传递给视图。

jQuery: jQuery:

 $.ajax({
      url: AppSettings.baseUrl + "BOM/getMultiBOM",
      type: 'POST',
       data: JSON.stringify(data)
 });

Controller:控制器:

[HttpPost]
public ActionResult getMultiBOM(multiBOMPostModel multiBomsPostModel)
{
       BOM bom = null;

       foreach (int value in multiBomsPostModel.bomsArray)
       {
           bom = db.BOMs.Find(value);
       }

       BOMViewModel viewModel = getViewModel(bom, null);
            
       return RedirectToAction("OpenMultiBOM", new { viewModel = viewModel, bom = bom });
}

public ActionResult OpenMultiBOM(BOMViewModel viewModel, BOM bom)
{
    viewModel.projectModel = new ProjectViewModel
        {
            project = bom.Project

        };
    return View(viewModel);
}

It is probably a bit of a mess.这可能有点乱。

I think the jQuery is necessary to pass the checkbox values to the controller.我认为 jQuery 是将复选框值传递给控制器​​所必需的。

When I use RedirectToAction to the method which then returns the view, the model is not being passed through, presumably as it is sending the model as a query string.当我使用RedirectToAction到然后返回视图的方法时,模型没有被传递,大概是因为它将模型作为查询字符串发送。

The view model is not simple and contains lists, IEnumerable s, and nested models.视图模型并不简单,它包含列表、 IEnumerable和嵌套模型。

Can anyone help with the most efficient way to redirect/return the view while passing the view model?任何人都可以帮助以最有效的方式在传递视图模型时重定向/返回视图吗?

Answer : I kept the ajax to post my checkbox values to the controller:我保留了 ajax 将我的复选框值发布到控制器

 $.ajax({
     url: AppSettings.baseUrl + "BOM/getMultiBOM",
     type: 'POST',
     data: JSON.stringify(dataArr),
 }).done(function (result) {
       location.href = "/BOM/OpenMultiBOM";
 });

In my controller, I assigned the posted values to a postModel and then stored them in TempData .在我的控制器中,我将发布的值分配给postModel ,然后将它们存储在TempData The key here was to return a Json value which would then allow the redirect on the client side to take place.这里的关键是返回一个 Json 值,然后允许在客户端进行重定向。

 public ActionResult getMultiBOM(multiBOMPostModel multiBOMPostModel)
 {  
        TempData["BOMs"] =  multiBOMPostModel;
        return Json("success");
 }

I then had another HttpGet method which would load after the page is redirected by the Ajax result and cast the TempData to an object.然后我有另一个HttpGet方法,它会在页面被 Ajax 结果重定向并将TempData转换为对象后TempData

[HttpGet]
public ActionResult OpenMultiBOM(int? BomMarkupMessage = null)
{
    var bomIds = TempData["BOMs"] as multiBOMPostModel;
}

I would persist the viewmodel server side, perhaps in a session variable, or perhaps as a TempData (TempData typically only lives until the next request), and pass a key for the session variable to the second controller in the case of session variable, or use the TempData directly in your view in the case of TempData.我会保留viewmodel服务器端,可能在会话变量中,或者可能作为TempData (TempData 通常只存在到下一个请求),并在会话变量的情况下将会话变量的键传递给第二个控制器,或者在 TempData 的情况下,直接在您的视图中使用 TempData。 This would avoid passing the whole object back and forth multiple times.这将避免多次来回传递整个对象。

So the way that i have done this before is to have an empty div in DOM.所以我之前这样做的方法是在 DOM 中有一个空的 div。

 <div id="partialViewContent"> <!-- Content will be loaded later. --> </div>

If you have a default view, you'll need to set it to load from URI using the below snippet.如果您有默认视图,则需要使用以下代码段将其设置为从 URI 加载。

 $("#partialViewContent").load("Controller/Action", function (response, status) { if (status !== "success") { console.log("An error has occured when attempting to load partial view."); } });

When you post to your controller action via JQUERY, have the action return a partial view with the model.当您通过 JQUERY 发布到控制器操作时,让该操作返回带有模型的部分视图。 (Assume model is relevant to each partial view). (假设模型与每个局部视图相关)。

Then when your ajax is complete, replace the content in partialViewContent with the POST response.然后当你的 ajax 完成时,用 POST 响应替换 partialViewContent 中的内容。

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

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