ASP.NET MVC - Execute controller action without redirecting

前端 未结 3 434
遥遥无期
遥遥无期 2021-01-11 15:31

I am trying to execute an action on a controller without redirecting to the associated view for that action. For a good example of what I am trying to achieve take a look at

相关标签:
3条回答
  • 2021-01-11 16:10
    • using System.Web.Mvc;
    • using System.Web.Mvc.Html;

      public ActionResult Index()
      {
          HtmlHelper helper = new HtmlHelper(new ViewContext(ControllerContext, new WebFormView(ControllerContext, "Index"), new ViewDataDictionary(), new TempDataDictionary(), new System.IO.StringWriter()), new ViewPage());
          helper.RenderAction("Index2");
      
          return View();
      }
      
      public ActionResult Index2(/*your arg*/)
      {
          //your code
          return new EmptyResult();
      }
      
    0 讨论(0)
  • 2021-01-11 16:16

    in your controller you must add bottom code:

    public ActionResult Index(string msg)
            {
                if (Request.Url.ToString().Contains("yourNewExampleUrlWithOutRedirect.com"))
                {
                    string html = "";
                    using (System.Net.WebClient client = new System.Net.WebClient())
                    {
                        client.Encoding = Encoding.UTF8;
                        html = client.DownloadString("https://NewExampleUrl.com/first/index?id=1");
                    }
                    Response.Write(html);
                }
    ...
    }
    

    your view must be empty so you add bottom code

        @{
    
            ViewBag.Title = "sample title";
            if (Request.Url.ToString().Contains("yourNewExampleUrlWithOutRedirect.com"))
            {
                Layout = null;
            }else
            {
                Layout ="~/Views/Shared/_Layout.cshtml"
            }
        }
        @if (Request.Url.ToString().Contains("yourNewExampleUrlWithOutRedirect.com")==false)
        {
        before view like :
    <div>hello world</div>
        }
    
    0 讨论(0)
  • 2021-01-11 16:34

    If you don't want "full page reloads" then you need to approach the problem slightly differently, using javascript to alter the page dynamically. A library such as JQuery might make manipulating the DOM a little easier.

    1. Display the popup dynamically using javascript.
    2. When the user hits OK/Submit on the popup, post the data back to the server using javascript, and have the controller you are posting to return some HTML.
    3. Append the returned HTML block (partial view) to an existing div containing playlist tracks.

    The most difficult part of this is the asynchronous post. Help with updating a div without reloading the whole page can be found in this question.

    EDIT - Example

    If you have a controller action (accepting POSTs) with the URL myapp.com/PlayList/AddSong/, then you'd set up JQuery to post to this URL. You'd also set up the data property with any form data which you'd like to post, in your case you'd add playistId and songId to the data property.

    You'd then use the result of the AJAX query (HTML) and append it to the existing playlist HTML on the page. So assuming that you want to append the partial view's HTML to a div with ID playlistDiv, and assuming that your partial view returns HTML which is valid when appended to the existing playlist, then your javascript will look something like this:

    var data = { playlistId: 1, songId: 1 };
    $.ajax({
      type: "POST",
      url: 'http://myapp.com/PlayList/AddSong/',
      data: data,
      success: function(resultData) {
          // take the result data and update the div
          $("#playlistDiv").append(resultData.html)
      },
      dataType: dataType
    });
    

    Disclaimer: I can't guarantee that this code will work 100% (unless I write the program myself). There may be differences in the version of JQuery that you use, etc, but with a little tweaking it should achieve the desired result.

    0 讨论(0)
提交回复
热议问题