Does PartialView do what I think should (and not)?

I have a div on an ASP.NET MVC page that I would like to dynamically populate (at the request of the user) using jQuery. I currently have jQuery placing a JSON call for my controller that returns a JSON object, and then I use Javascript to assemble the HTML manually, which is then placed in a div.

Wait a minute. Wouldn't it be much simpler to get the controller to create HTML on its own (using its own control (.ascx file)) and then just return the line to be placed in the div?

Lighter, generously!

My current attempt includes the following javascript:

$('#MyDiv').load("/MyController/GetList");

calling the following controller method:

public PartialViewResult GetList()
{
    ... create model ...
    var result = PartialView("CategoryList", model);
    return result;
}

, . ( ), Firebug Net ( Firebug Console ).

, Debug.Print .ascx , jQuery PartialView .

PartialView ( PartialViewResult), ?

.

, ... .

+3
5

, , , ( ),

<html><head><title></title></head><body>

</body></html>

. .

, - , , Preview 3 → Preview 4 → Preview 5 → Beta → RC1, , , , .

- , ...

+2

. , jquery load()?

+1

, - jQuery script? , jQuery MVC. .

0
, , Fiddler2 , ? , , , MVC. -, .

/MyController/GetList, HTML?

"foo.html" , HTML, . jQuery, foo.html:

$('#MyDiv').load("/foo.html");

It works? One thing you can do with the jQuery load method is the CSS selector. So, as a workaround, you can wrap the partial with an HTML header, body tags, as in your other answer, but then add a CSS selector to the load method to capture only the part you want, for example:

$('#MyDiv').load("/MyController/GetList #stuff-i-want");
0
source

It just works for me. I created a sample

View

<div id="sam"></div>

<% var url = Url.Action("GetList"); %>
<script type="text/javascript">
    $(document).ready(function() {
        $('#sam').load('<%=url%>');
    });
</script>

Partial view - GetList.ascx

<%@ Control Language="C#" Inherits="ViewUserControl<IEnumerable<string>>" %>
<ul>
<% foreach (var s in Model) { %>
    <li><%=Html.Encode(s)%></li>
<% } %>
</ul>

Controller

[AcceptVerbs(HttpVerbs.Get)]
public PartialViewResult GetList()
{
    return PartialView(new[] { "Hello", "Foo", "Bar" });
}

Hope this helps.

0
source

Source: https://habr.com/ru/post/1702880/


All Articles