I am developing a web site using MVC 3 razor, in which I have the following situation:
- A controller method:
public ActionResult MyControllerMethod(int parameter){
''go to bd a do some staff
IList<My_Custom> datafromDB = MyService.GetData(parameter);
'returns to my string tuped view
return View(datafromDB );
}
- A strong typed view in which I use JavaScript:
@model IList<My_Custom>
<script type="text/javascript">
function getData()
{
var parameter = document.getElementById('myParamater').value;
$.get('/MyController/MyControllerMethod/' + parameter, function (data) {
loadData();
}
);
}
function loadData()
{
clearData();
datos = @Html.Raw(Json.Encode(Model));
//do some stuff with datos
}
</script>
The JavaScript call to the controller works fine but my problem is that the string typed view seems like is not taking the new value for the @model, so keeps loading the same information.
Although I have debugged the action controller and it returns different data every time.
So is there any way to refresh the Model value of a string typed view?
I also tried to process the data value of that line
$.get('/MyController/MyControllerMethod/' + parameter, function (data) {
but I wasn’t successful doing that.
Your action return view, that’s why nothing works. How your code works:
htmlwithjson(all this data are in memory),then you access
jsonfrom first stepHow it must work:
Json)datapublic ActionResult YourJsonAction(int parameter) { IList datafromDB = MyService.GetData(parameter); return Json(datafromDB, JsonRequestBehavior.AllowGet); } function getData() { var parameter = $('#myParamater').val(); $.get('/MyController/YourJsonAction/' + parameter, function (data) { //data from YourJsonAction }); }