Search
 
SCRIPT & CODE EXAMPLE
 

JAVASCRIPT

mvc asp.net partial view from js

//You can call a Partial View through AJAX

<div id="containerId"></div>

$.ajax({
    type: "Get",
    url: '<Your url>/GetView',
    data: mydata,
    contentType: "application/text; charset=utf-8",
    dataType: "text",
    success: function (data, status) {
      	//Use append to add it to the div and not overwrite it 
		//if you have other data in your container
        $('#containerId').append(data);
    },
    error: function (err) {
        console.log(err);
    }
});

//In C#

/// <summary>
/// Renders a single view.
/// NOTE : PARTIAL VIEW CANNOT RENDER MULTIPLE VIEWS!
/// Instead loop through them.
/// </summary>
/// <param name="obj">JSON object containing input data</param>
/// <returns></returns>
[HttpGet]
public ActionResult GetView(string obj)
{
    //Parse the object into a model
    try
    {
        MyModel model = (new JavaScriptSerializer()).Deserialize<MyModel>(obj);
        return View("<Your View name>", obj);

    }
    catch (Exception ex)
    {
        return Json(ex.Message, JsonRequestBehavior.AllowGet);
    }

}
Comment

PREVIOUS NEXT
Code Example
Javascript :: download canvas to png 
Javascript :: Auto increment in realtime database with javascript 
Javascript :: copy folder in nodejs 
Javascript :: object.assign in express 
Javascript :: function with .map javascript 
Javascript :: javascript function syntax 
Javascript :: js max number in array mdn 
Javascript :: jquery get parent element 
Javascript :: flatlist react native horizontal 
Javascript :: console.group in javascript 
Javascript :: js module pattern 
Javascript :: javascripts events 
Javascript :: curl to javascript fetch 
Javascript :: how to write unit test cases in react js 
Javascript :: javascript callbacks 
Javascript :: find the length and the last character of string in js 
Javascript :: double bang js 
Javascript :: use node js as backend with angular frontend 
Javascript :: js date toisostring with timezone 
Javascript :: react code input 
Javascript :: how to read if a person has send a message on discord.js 
Javascript :: Detect Mobile / Computer by Javascript 
Javascript :: vue resources post 
Javascript :: can we pass variable to a object 
Javascript :: TypeError: Expected a string but received a undefined 
Javascript :: next greater element javascript using stack 
Python :: abc list python 
Python :: postgres django settings 
Python :: python b to string 
Python :: python selenium get image src 
ADD CONTENT
Topic
Content
Source link
Name
8+4 =