MVC 4 Accessing the Parent Model object in the Child Post method - asp.net-mvc

Using MVC 4 framework, I have a Parent and Child View (put Child in Html.Partial). I used Jquery dialog in showing the Child View. In my Child View, I have a Submit button. I want the data that I inputted in the Parent View to be retained upon posting back the Child View. Is it possible to access the Model object of the Parent View, so that I can still pass the Parent Model Object in the HttpPost method of the Child View?
Here's my sample code for ParentView:
#model Model.PurchaseOrderModel
#using (Html.BeginForm())
{
<div>Parent</div>
<div id="ItemDialog" style="display: none;">
#Html.Partial("ChildView")
</div>
}
Here's my sample code for ChildView:
<div>
<div>Child</div>
<div>
<input type="submit" id="btnSaveItems" text="Save" value="Save Items" />
</div>
</div>

Related

MVC load Partial View data when it is called

I am developing an MVC Bootstrap Application.
I have a Parent View, that calls a Modal Bootstrap Partial View. That Partial View is loaded when User clicks a buttom.
The Partial View calls Controller Method that goes to the server.
Here is my example. In my Parent View a have this call.
<div class="modal fade" id="myModalRecomendIt" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
#Html.Partial("~/Views/UploadSearch/_RecomendIt.cshtml");
</div>
Inside the Partial View I have something like this.
<div class="modal-body">
<div class="col-md-10">
#Html.Action("GetUsers", "Users")
</div>
</div>
It Works fine...
The problem I found is that The Controller Method I call in Partial View is called when Parent View is loaded. So it takes too long to render.
What I need is to call those methods when Partial View is called.
Is it posible?
Thanks..

Partial View is null

I'm learning to use Partial Views in MVC. It's working if I just have some simple text in the Partial View, but when I'm trying to use data from the Model, I get some problem! I guess I'm missing some point and need some guidance.
I'm trying to use a Partial View inside HomeController index file.
The code inside the index file:
<div class="row">
<div class="col-md-3" id="listOfNames">
#Html.Partial("ListPersonLayoutPartialView")
</div>
</div>
And the Partial View:
#model IEnumerable<TestAjaxAutoSuggest.Models.Person>
#foreach (var item in Model) {
<div class="personBlock">#item.Name</div>
}
<p>Test</p>
And finally the action method in the Home controller:
[HttpGet]
public ActionResult ListPersonLayout()
{
return PartialView("ListPersonLayoutPartialView", db.People.ToList());
}
I guess something is missing, like the how the data from the Controller connects to the Partial View!?
You need to use Html.Action here not Html.Partial,if you want to render partial view Html.Partial then you have to pass Model as well from Main view, which are not passing, so Model is null and additionally Html.Partial does not call action, for calling action you have use Html.Action which will in return renders partial view:
#Html.Action("ListPersonLayout")
you need to understand difference of these two, for that you can see this post
You should call ListPersonLayout and not ListPersonLayoutPartialView as follows:
<div class="row">
<div class="col-md-3" id="listOfNames">
#Html.Partial("ListPersonLayout")
</div>
</div>

nested MVC views and partial views sitefinity

I have the view below which has a partial loaded onto it. Now I would like to update the partial but not reload the actual view. I am using MVC in sitefinity so do not have the use of master pages.
<div class="pr-nav">
<div class="btn btn-primary">
User Guide
</div>
<div class="btn btn-primary">
Key Info
</div>
</div>
<div class="container">
#Html.Partial("subListPartial")
</div>
You could do an ajax request and return a partial view as an action result in your controller and replace the partial.
Here is a example of an action with a partial result:
[HttpPost]
[ValidateAntiForgeryToken]
public PartialViewResult User(EditUser model)
{
return PartialView("_EditAccountForm", model);
}

I am attempting to return a model from a form that shows the details of said model but am returning a null value

My form is not returning a null model.
I am viewing the details of a specific model on a Details page and then, if a user presses the SubmitPost button, the view should return the model.
View
#model MyApp.Models.MyModel
#using (Html.BeginForm("ApplyUsingDetails", "Controller", FormMethod.Post))
{
<fieldset>
<legend>Course</legend>
<div class="display-label">MyModel ID</div>
<div class="display-field">
#Html.DisplayFor(model => model.CourseID)
</div>
<div class="display-label">Title</div>
<div class="display-field">
#Html.DisplayFor(model => model.Title)
</div>
etc. . .
<p><input type="submit" value="SubmitPost" /></p>
<more misc information for the view>
</fieldset>
}
Action
[HttpPost]
public ActionResult ApplyUsingDetails(MyModel model)
{
// when I try to access model here it comes up null
}
To give some context: I am using this form to allow a student to view the details of a class and then apply to the class at the click of a button.
Any clue as to why my model would be null?
Based on the sample you provided, it doesn't look like you are including any of MyModel's fields in the form. As a test, try including a hidden field or use #Html.HiddenFor(model => model.CourseID) and see if you have any values posted.
DisplayFor HTML helper will simply return a string that represents the Property value usually. If your Property is a boolean type, it wll render it as a disabled checkbox. As long as you dont have any input controls you wont be getting the value. So in this case, you can simply use the #Html.HiddenFor HTML helper method.
HTML.HiddenFor Helper Method generates HTM Markup for an input element of type hidden with the id and name same as the Expression
#using (Html.BeginForm("ApplyUsingDetails", "Controller", FormMethod.Post))
{
<div class="display-label">MyModel ID</div>
<div class="display-field">
#Html.DisplayFor(model => model.CourseID)
#Html.HiddenFor(m=>m.CourseId)
</div>
<input type="submit" value="Save" />
}
Now the Value of Course id will be available in your HTTPPost action method.
You need to include input elements in your form so that they can be posted to your controller and bound to your controller's model. Model Binding in ASP.Net MVC takes the form values that you submitted and looks for a property on your controller's model with the same name and tries to set the propery with the form value. No input fields in your form means nothing for Model Binding to stick into the controller's model.
I would suggest using readonly input elements styled with css to look like div.display-field.
<div class="display-label">Course ID</div>
<div>
#Html.TextBoxFor(model=>model.CourseID, new{readonly="readonly", #class="display-field"})
</div>
Wrapping the input in a div may or may not be needed depending on other styles on the page.

How do I pass the right model to the Partial View?

I'm getting this troublesome error in my view:
The model item passed into the dictionary is of type 'ContactWeb.Models.ContactListViewModel', but this dictionary requires a model item of type 'ContactWebLibrary.Contact'.
on this line of code: #{Html.RenderPartial("Form");}
I'm using #model ContactWeb.Models.ContactListViewModel at the top of this file.
Here's my view:
#model ContactWeb.Models.ContactListViewModel
<h2>Edit</h2>
#{Html.RenderPartial("Form");}
#using (Html.BeginForm())
{
<fieldset>
<legend>Select roles for this user:</legend>
<ul>
#foreach(var role in Model.AllRoles)
{
<li><input name="Roles" type="checkbox" value="#role" />#role</li>
}
</ul>
<input type="submit" />
</fieldset>
}
The error tells you have to pass the right model to the Partial View.
Because you didn't pass anything, the MVC framework uses the default model which is the view which called to the Partial model.
This will solve things out:
#{Html.RenderPartial("Form", new ContactWebLibrary.Contact());}
It has nothing to do with RenderPartial VS Partial.

Resources