I am making WCF service call using MyViewRequest view fields inside HttpPost ActionHandler. The goal is to show response using partial view, MyViewResponse
In brief I need to achieve these two items-
- Disable load of partial view on first load.
- Display Response (along with Request) after service call.
MyViewRequest.cshtml
@using (Html.BeginForm())
{
@Html.ValidationSummary(false)
//html code
}
</div>
<div id="dvResponse">
@Html.Partial("MyViewResponse");
</div>
Partial view: MyViewResponse.cshtml
@model MvcApplication3.Models.MyModel
@{
ViewBag.Title = "MyViewResponse";
}
<h2>MyView</h2>
@Html.Label(Model.MyName, "My Name")
This was pretty straight forward in Asp.Net using userControl, But stuck up here, How can we achieve this in MVC3.
I think the best way is to transfer your data using ViewModels. Let’s assume you want to have an app something like stackoverflow where you have a question and user can post an answer and it will be shown after the post along with the question.
in your
GETaction, you show the question. Get the id from the url and get the Question details from your service/repositary.Assuming
yourService.GetQuestionFromIDmethod returns an object of PostViewModel with the propety values filled. The data can be fetched from your database or via a WCF service call. It is up to you. AlsoyourService.GetAnswersFromQuestionIDmethod returns a list of PostViewModel to represent the Answers for that question. You may put both these into a single method calledGetQuestionWithAnswers. I wrote 2 methods to make it more clear.Now in your Show view
And your Partial view will be strongly typed to a collection of
PostViewModelHandling the postback is simple (HttpPost)