Displaying blog categories in partial view on layout page - c#

Currently I have a partial view where I manually display all my blog categories with links. I would like to make it dynamic by pulling from the database. I am not sure how to accomplish this within a partial view. I would even be willing to do it within the actual _layout page if it's easier that way.
Here is what I have right now.
_Categories.cshtml
<h2>Categories</h2>
<hr/>
<p>
ASP.Net MVC<br/>
Ruby on Rails<br/>
</p>
I would like to create these links dynamically as opposed to hard coding.
_Layout.cshtml
#Html.Partial("_Categories")
The main problem is there is no controller for the layout of a partial which is why I can't figure out how to go about it.
Thanks in advance for any help.

Create a controller action named ListCategories in BlogController (or in a new CategoryController). Add all the categories to the ViewBag in the action by querying them from your back-end database
public ActionResult ListCategories()
{
ViewBag.Categories = db.Categories;
}
And use a #foreach loop in the view for the action ListCategories.cshtml:
<h2>Categories</h2>
<hr/>
<p>
#foreach(Category c in ViewBag.Categories)
{
#c.Name<br/>
}
</p>
Finally, change your _Layout.cshtml to point to this action:
#Html.Action("ListCategories")
// or #Html.Action("ListCategories", "CategoryController")

Related

Render a View inside a View in Asp.Net mvc

How do I render a full fledged view (not partial view) inside another view?
Scenario, I have different controller and want the exactly same view to render which is already there under other controller with different layout.
I have Wishlist page in Home Controller which shows list of added products, and when user logged in , when I click on wish list it also show me navigation when user is signed in.
How would I do that??
Not many developers know about this but you can use RenderPage, it's specifically designed for that purpose(to render an MVC view inside another view)
#RenderPage("~/Views/Shared/SampleView.cshtml")
You can still create or use a partial view with its own controller and use the RenderAction()
[ChildActionOnly]
public ActionResult ActionPartialView(string p1)
{
//code...
return PartialView();
}
The above code can be in any controller, its own controller, just call it in razor with that controller.
Razor:
#{ Html.RenderAction("Index", "Home"); }
Hope that helps

Pass data from one view to another view -MVC

What would be the best method to transfer data from one view to another view? or is this bad practice?
The reason is that i want to display #model.count() from one view to the homepage, basically a summary of all the views on the homepage. There is tempdata, html.helper etc but id like to know the best and most reliable method. Thus avoiding problems further on down the line
here is the code in question
product.cshtml
<p>Total amount of Products = #Model.Count()</p>
i basically want that same value to display on the homepage
You'd have to refresh the homepage for the value to actually 'update.' At that point you may as well pass the value back in as a parameter, but I think that's not what you're hoping for.
It probably isn't the cleanest solution, but I would think you'd want your text on the main page to be settable via some javascript/jquery.
Main Page has this snippet:
<form id="PassBackVals"
<input type="hidden" name="ProductCount" value="0">
</form>
<script>$( ".ProductCount" ).change(function() {
$("$CurrentProductCount").text = $(".ProductCount").val;
});
</script>
and the Area displaying the text itself on the main would have something along the lines of
<p> I'm currently showing <div id="CurrentProductCount">0</div> Products </p>
Then in your product.cshtml, you'd have
<script>$(".ProductCount").val = #Model.Count()
$( ".ProductCount" ).change();
</script>
Ok i looked into other methods and resolved the issue by moving the code behind the viewbag into a public method below
[OutputCache(NoStore = true, Location = OutputCacheLocation.Client, Duration = 10)]
public ActionResult UpdateTotals()
{
JobController j = new JobController();
ViewBag.JobCount = j.JobCount();
ProductController p = new ProductController();
ViewBag.ProductCount = p.ProductCount();
return PartialView("UpdateTotals");
}
then i added the viewbags into a partial view and using setinterval() javascript,
<div id ="UpdateTotals">
#{ Html.RenderAction("UpdateTotals");}
</div>
<script>
setInterval("$('#UpdateTotals').load('/home/UpdateTotals')", 10000);
</script>
i was able to refresh the data constantly to my desire without the mess of pulling data through views via javascript.
hopefully this will help anyone in future :)

One controller, multplie views, multiple forms

The goal is to have one page with a wizard. Each step of the wizard is a partial view containing a form. I have only one controller (Insurance) with an action for each view. The actions receive the posted data and return the viewmodel for the next step, or the viewmodel of the current step containing the error details.
The page (Index.cshtml) has partial views, rendered as
#Html.Partial("~/Views/Shared/_RegistrationCode.cshtml")
and the partial view itself contains a form, rendered as
#using (Html.BeginForm("RegistrationCodeDetails", "Insurance", FormMethod.Post)) {
and a
<input type="submit" name="nextButton" value="Verder" class="btn btn-success" />
within the form to submit it.
The code works as intended up to the point where the first action returns the viewmodel for the next step (partial view _Product) using
return PartialView("_Product", productViewModel);. The ActionResult is not sent to the partial view, but rendered as a full view, so the result is a partial being rendered as the only thing on the screen.
I've fiddled with #using (Ajax.BeginForm("RegistrationCodeDetails", "Insurance", new AjaxOptions { UpdateTargetId = "articleProductOutput", HttpMethod = "Post" })) { but the data is not rendered in the second wizard step partial.
Edit:
We've decided to take a different approach: One page, one controller and basically one viewmodel. The initial data is rendered right away, data depending on other steps in the wizard is retrieved using JSON and partial views.
Unless you mark your partial view as [ChildActionOnly], it won't load in the same page!
you Partial view should look like
[ChildActionOnly]
public ActionResult _ParialView1()
{
//TODO: Add the required code here
}
//and your PartialView should be included in the main view as :
#{Html.Action("_PartialView1","Controller1");}
Thanks and hope this helps!

MVC.Net Action link to update partial

I'm using partial views within an MVC.Net website to display content from a database.
I want to be able to offer as WYSIWYG as possible an interface for modifying that content.
Ideally I'd like to be able to put an #Html.Action in whatever page I need some content on. Then automatically offer an "Edit" link for authenticated users.
Once the edit link is clicked, I want to show the same page as they were already on. (From a different controller). But with the partial replaced with it's "Edit" state alternative.
Obviously at the moment, putting an ActionLink in the partial navigates away from the current page and displays the Edit view full screen.
The Controller "SiteCMSController" looks up the id from the db/cache and inserts it in the page.
If the user is an admin, it adds an edit link.
<div>
#Html.Action("_Content", "SiteCMS", new { id = 1 })
</div>
results in
<div>
Some text pulled in from the database.
<br>
Edit this content
</div>
(Off the top of my head code.)
use some ajax
<div>
#Ajax.Actionlink("_Content", "SiteCMS", new { id = 1}, new AjaxOptions { UpdateTargetId = "someDiv"})
</div>
UpdateTargetId, this is were your partial will be stored, you have to make a div with the id "someDiv", or something else ofc :)

Render Partial View from other controller

Is there a way to render inside my view of controller A a partial view from other controller B?
Edit: I wrote a partial view that is good for only two controllers and I don't want to copy it to their both Views folder.
I want The partial view to be displayed each time the View is rendered not after something happens.
You can share views between controllers by putting them into the Views/Shared folder. Each controller can then render that view by name.
You can render a partial view (which can be shared between controllers as in (1)) within the current view using Html.Partial().
You can use Html.Action() to invoke an action on a different controller and render the results within the current view.
You can use AJAX to load a partial view from a different controller after the page has been rendered.
#Html.Partial("~/Views/ControllerB/Index.cshtml")
Yes,
return PartialView("/path/view.cshtml");
You just need to work out the path part.
Alternatively you can put the partial view in views/shared then just return :
return PartialView("view.cshtml");
#model YourModelNamesapce.ModelName
#{
ViewBag.Title = "Edit";
Layout = "~/Views/Shared/_LayoutForPartialViews.cshtml";
}
<table>
<tr>
<td>
#Html.LabelFor(model => model.fieldname)
</td>
<td>
#Html.DisplayFor(model => model.fieldname)
</td>
</tr>
<tr>
<td>#Html.Action("PartialViewAction", "Controller", new { id = Model.id })</td>
</tr>
</table>
Just a side note as i found this thread searching for the same question but the answers weren't working: in Orchard CMS modules you cannot use the neat solution posted by Pittfall, you have to use relative paths to return partial views. Lets say you have a controller
Controllers/SiteController.cs
and you want to return the partial view
Shared/MessageList/Items
then in your action methods you need to write
return PartialView("../Shared/MessageList/Items");

Categories

Resources