Rendering Partial View in code MVC Razor

前端 未结 4 1659
攒了一身酷
攒了一身酷 2021-02-12 10:33

I\'m using MVC 3 Razor to make a simple CMS for practice purposes, and the idea is that I\'m creating a few partial views.

I\'m wanting to do a database lookup, and see

4条回答
  •  广开言路
    2021-02-12 11:05

    Html.RenderPartial("partialview name", Model.class, new ViewDataDictionary { TemplateInfo = new TemplateInfo { HtmlFieldPrefix = "classname" } });
    
    This code can be used to render the partial view in apge.
    HTMLfiledprefix is defined to keep the data available in the model
    
    You can use tis code to load a partial view on a button event using ajax
     function partialview() {
            var url = '@Url.Action("action", "controller")';
            var data = $('#frm').serialize();
    (to serialize the data in the model before posting to the action)
            var finaldata = data;
            $.ajax({
                type: "post",
                url: url,
                data: finaldata,
                async: false,
                contentType: "application/json; charset=utf-8",
                error: function (xhr) {
                    errorRedirecttoErrorController(xhr.error);
                },
                success: function (data) {
                    $("#DIVID").html(data);
    (div to which the partial view to be loaded)
                }
            });
        }
    

提交回复
热议问题