Rendering view in ASP.Net MVC

2019-05-31 14:26发布

问题:

In my code I am using a partial view (call it PV1)

<div id="div_1">
    <% Html.Partial("PV1", Model); %>
</div>

and in that partial view I am using "Ajax.BeginForm" to redirect it to a particular action, hence it is doing so ...

using (Ajax.BeginForm("action1", "Forms", null, new AjaxOptions { UpdateTargetId = "div_1" }, new { id = "form1" }))
                {
                    Response.write("I am here.");
                }

public ActionResult action1(XYZ Model)
{
        //
       return PartialView("PV1", Model);
}

at the last line of action I am again calling the same partial 'PV1', hence it is doing this too ...

but when rendering the view it don't print or do the steps written with in partial view, it override them with and show nothing ...

回答1:

Html.Partial actually returns the result of rendering the view, you want to do <%= Html.Partial() %> or <% Html.RenderPartial(); %>

Html.Partial() returns the Html and thusly must be output on the page via <%= %> and Html.RenderPartial() uses Response.Write to output onto the page and can be used with <% %>.



回答2:

This is not what you would use Ajax.BeginForm for. That helper is used to create actual <form> tags that will later be submitted to the server using MVC's unobtrusive ajax (so you still need some kind of a trigger action - a button, javascript - anything that submits the form).

I'm am not very clear on what you're trying to achieve. If you want to load a partial view with ajax, I would suggest using something like jQuery's ajax load method.