Partial View is null - asp.net-mvc

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>

Related

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);
}

In an MVC view, what controller and method is called if none is specified in the form tag?

I have a MVC project I inherited. On the views, there's no route (controller and action) specified in the BeginForm tag. The view renders correctly so I assume it is picking a default route.
My question is how does it know what route to use if one isn't specified? What's the best practice here: should you specify a route or let it default?
So the view is Views/Config/WorkCodes.cshtml and the tag is
Html.BeginForm()
It goes to the controller ConfigController.cs and calls action WorkCodes(). If I was doing the project, I would have wrote
Html.BeginForm("WorkCodes", "Config", FormMethod.Post)
How does MVC know which controller and action to use without specifying it?
WorkCodes.cshtml
#{
ViewBag.Title = "Work Codes";
}
#using (Html.BeginForm())
{
<div>
<table style="width: 100%;" class="trHoverHighlight">
<tbody>
<tr>
<td>
<br />
<div>
<button id="buttonCreateNew" type="button">Add New</button>
<button id="buttonReturn" type="button">Return</button>
</div>
</td>
</tr>
</tbody>
</table>
</div>
}
ConfigController.cs
public ActionResult WorkCodes()
{
return View(Rep.GetWorkAll(true));
}
Here's where the view is called from in another view:
#foreach (var itm in (List<string>)ViewBag.ListObjects)
{
<li>
Work Codes
</li>
}
The View is generated from performing HTTP GET to the WorkCodes controller action, thus by default the form generated in that view performs an HTTP POST to a controller action with the same name.
Here's the MSDN docs.
BeginForm(HtmlHelper) Writes an opening tag to the response.
The form uses the POST method, and the request is processed by the
action method for the view.

Razor implementing template to wrap objects

The goal es to have a template :
<div class="container">
<div class="row">
<div class="col-lg-11">
#RenderBody()
</div>
</div>
</div>
and in normal cshtml:
#using ("template") {
// Any html razor content
}
#using ("template") {
// Other different html razor content
}
Is there anyway to achieve this using cshtml for templates (no c# or html writers) ?
I know it can be accomplished with partial views, but I want to avoid to create a partial view for every piece of content I want to wrap with that template.
Thanks.
Partial Views or js Templates as mustache or handlebars.
I do not think you can assign Template with razor syntax # to another server variable inline
you also can create Razor Helpers but if you do not want to use partialViews not sure if u want to do that
#helper MyTemplate(){
<div class="container">
<div class="row">
<div class="col-lg-11">
#RenderBody()
</div>
</div>
}
and use it as #HTML.MyTemplate

Html.BeginForm in partial for a different controller

I have the following code:
<% using (Html.BeginForm("AddComment", "Comments", FormMethod.Post)) { %>
<div id="New_Comment">
<textarea name="newComment" id="newComment">Add comments</textarea>
<input type="submit" value="Add" />
<div><span class="text_grey">Attach:</span>File Link</div>
</div>
<%} %>
This is in a partial rendered by the MyPage controller. For some reason the action on the form comes out blank, if I reference a method on the MyPage controller it works fine what I want to do is point to a different controller with my form.
To solve this issue I simple added in an area route value like so:
new { area = "" }
With the empty string directing the route to the default area.
1) Is your "Comments" action marked as being a POST action?
2) Also
Try just doing:
<% Html.BeginForm("AddComment", "Comments"); %>
// Html and script
<% Html.EndForm(); %>
I know that there shouldn't be difference between what you have and what I suggest, but it's worth a try.

partial views in ASP.NET MVC

How create View page which is composed of three partial view pages? I am using ASP.NET MVC
//
// GET: /Partial/
public ActionResult View1()
{
var upit = from i in proba.name
select i;
return PartialView("upit",proba.name);
}
<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">
<h2>Index</h2>
<div><%Html.RenderPartial("View1",proba.name); %></div>
</asp:Content>
Why this code return error: Object reference not set to an instance of an object.
I think you want to use RenderAction, not RenderPartial. View1 is an action, not a view. It returns the partial view, upit. Note that this requires MVC2 (or MVC1 + futures). You also probably want to decorate the action with the ChildActionOnlyAttribute unless you intend to call it from AJAX as well.
[ChildActionOnly]
public ActionResult View1()
{
var upit = from i in proba.name
select i;
return PartialView("upit",proba.name);
}
asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">
<h2>Index</h2>
<div><%= Html.Action("View1"); %></div>
</asp:Content>
The reason you are getting the specific error is that the proba variable isn't defined in the view, nor does it need to be. In your case, the action determines the model being passed to the partial view so there's no need for any data to be passed. You could only pass it via query parameters anyway when rendering an action.
Inside your view page you would want to make use of the RenderPartial method.
Example
Say I had 3 partial views called "View1", "View2" and "View3". If I wanted to render all 3 of these views to make up the content of my view then I would do something like:
<div id="section1">
<% Html.RenderPartial("View1", Model.Table1) %>
</div>
<div id="section2">
<% Html.RenderPartial("View2", Model.Table2) %>
</div>
<div id="section3">
<% Html.RenderPartial("View3", Model.Table3) %>
</div>
I assume you would have a MasterPage which your view derives from in order to take care of the other necessary markup. The above would just be placed inside the Content section of your derived view.

Resources