0

MVC4, on the click on dropdown item javascript function is called in view's 'scripts' section. Function makes ajax call to controller action, Json data returned. I need to pass some returned values to Html.Partial() to render in . How to accomplish that? Those value "do not exist in the current context" for Html.Partial().

VIEW : MyView

<div>@Html.DropDownList("listId", list, new { onChange=showText() }</div>
<div id="divMyText" ></div>

@section scripts{

function showText()
{
    var val1 = 1;

    $.ajax({
       type:"POST",
       url: "/Home/MyAction",
       data: {parm1:val1},
       success: function (result){
          renderMyView(result.id);
       }
    });
}

function renderMyView(id)
{
   $('#divMyText').html('@Html.Partial("MyView", new MyViewModel (id))');  // id here is Not 'visible' for MyViewModel. 
}

}

CONTROLLER actions:

public ActionResult MyAction(int parm1)
{
   .......
   return Json (myObject);
}

public ActionResult MyView (int id)
{
    MyViewModel model = new MyViewModel(id);
    return View(model);
}

How to pass id value to MyViewModel in Html.Partial statement ? Thank you

1
  • i am afraid you cannot do like this Commented Oct 11, 2014 at 16:57

1 Answer 1

1

you need to combine them on the controller side. Change my action to

public PartialViewResult MyAction (int id)
{
    MyViewModel model = new MyViewModel(id);
    return PartialView("PartialName", model);
}    

then in your script instead of calling your render view function

$('#divMyText').html(result);

this will take the returned partial view with the tied model and put it into the div

Sign up to request clarification or add additional context in comments.

1 Comment

And no need for action MyView() ?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.