简体   繁体   中英

MVC: How to have a View display from within a div

In my MVC project indide one of my views I have some JQuery that pops up a dialog whos content is defined within a tag

Is there a way that I can ensure the contents of the is a View/PartialView which is populated from my controler along the lines of:

    public ActionResult PopulateDivContent()
    {
        MyBusinessEntity entity = GetEntity();
        return PartialView("SingleRow", MyBusinessEntity);
    }

How would I go about doing this?

I have some jQuery that loads a partial view into a page. The partial view is an upload form for attaching images.

Here is the js/jQuery:

            function addAnImageClick() {
                // hide this
                $("a#addImage").hide();
                // show loading gif
                $("img.addAnImageLoadingGif").show();
                // invoke ajax get
                $.get("/FileManager/GetUploadForm", 
                    { postUrl: "/Announcements/UploadImageToAnnouncement", modelID: <% =Model.ID %>, multipleFiles: false }, 
                    function(data) {
                    // show upload form
                    $("div#imageUploader").html(data);
                    $("div#imageUploader").show();
                    // hide loading gif
                    $("img.addAnImageLoadingGif").hide();
                });
            }

As you can see i hide the link that the user clicks to initiate this function, then show the loading gif for UI feedback, then invoke the ajax call to the controller. I also pass some values to the action. Once the response is back i load the content into the div with this line: $("div#imageUploader").html(data); . The div was hidden with css display:none which then i show and finish by removing the loading gif from visibility.

Here is the action:

    //
    // GET: /FileManager/GetUploadForm/?uploadAction=actionName&modelID=1234&multipleFiles=true
    public class GetUploadFormDTO
    {
        public string PostUrl { get; set; }
        public int ModelID { get; set; }
        public bool MultipleFiles { get; set; }
    }
    [KsisAuthorize()]
    public ActionResult GetUploadForm(string postUrl, string modelID, string multipleFiles)
    {
        GetUploadFormDTO model =
            new GetUploadFormDTO()
            {
                PostUrl = postUrl,
                ModelID = Convert.ToInt32(modelID),
                MultipleFiles = Convert.ToBoolean(multipleFiles)
            };

        return PartialView("UploadFormPartial", model);
    }

Hope this helps. :D

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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