Call the Controller’s Action method of the Partial View - asp.net-mvc

I have a partial view that fills a select option.
From another view, I need to call the previous partial view to display my <select> <option> /> component.
However, I need to call the partial view WITH the associated controller.
Note : I am using .NET Core 6.
Here is my Controller :
public async Task<IActionResult> getYearMonth()
{
var result = await _context.MonthlyConsumption.Select(t => new { t.Time.Year, t.Time.Month }).ToListAsync();
return PartialView(result);
}
And my PartialView :
#model IEnumerable<TestLogin.Models.MonthlyConsumption>
<select name="mois" id="mois">
#foreach(var item in Model){
<option value="#Html.DisplayFor(modelItem => item.Time)">#Html.DisplayFor(modelItem => item.Time)</option>
}
</select>
I want to call the Controller’s Action method of the Partial View
I tried using #Html.Action, #Html.RenderAction but I can't use it.
#Html.Action is not working because i am using .NET 6
And I don't know why #Html.RenderAction is not working : IHtmlHelper does not contain definition for RenderAction.
I looked up why I was getting this error message. Some people say you have to import System.Web.Mvc but I can't get it to import in .NET 6.

#Html.RenderAction and #Html.Action are not available in Asp.Net Core, There are two methods can achieve what you want.
The First method is to use View Component. It is a new feature in Asp.Net Core and is also the officially recommended method, More details you can refer to this link.
The Second method is to use ajax, you can use ajax to access action, Then in success method, you can use js to add this partial view to the main View.

Related

MVC RenderAction return only renders partial part

I have this contact form which I want to use on two pages (two views) in MVC 5.x (razor viewengine) So I have put the form in an partialview called _Contact and I have read that RenderAction is the best approach if you do not have the required data for the partialview in your model and if it is more standalone (seperate from the rest of the view)
So I call it like this:
#{ Html.RenderAction("SendMail", "Uk");}
My Uk controller has these two methods:
[HttpGet]
public PartialViewResult SendMail()
{
return PartialView("_Contact");
}
[HttpPost]
public PartialViewResult SendMail(FormCollection fc)
{
// send mail using values out of the form (sorry did not feel like building a complete model for it
ViewBag.Succeed = true;
// if smtpclient could not reach server etc. it returns false
return PartialView("_Contact");
}
it all works, but the PartialView is only rendered, not on the placeholder where i call the RenderAction. It works all great, but after the post it just displays the partial view and not the "parent view" and the shared layout view etc. I hope that I made myself clear. Please let me know if I need to add more info.
This is the BeginForm from my shared view:
using (Html.BeginForm("SendMail", "Uk", FormMethod.Post))
It will not work as expected for your current code, because when you post the form, it returns Partial View not complete View. If you want to get only partial view then you have to submit your form via Ajax.
In ajax's success handler you will get HTML of your partial view and that you can put in a DIV tag of partial view container.
This link will give you a better idea about Posting Partial View via Ajax.
ASP.NET MVC Partial view ajax post?

ASP.Net MVC Partial View Model Binding

I'm very new to MVC and I am looking to put a list of links on the main layout(master page) based on database table. I'm sure I read before that you shouldn't try to load models on the master page but use Partial Views instead (correct me if I'm wrong).
I've looked on Google and on other questions here but they only seem to talk about passing data from a main view to a partial view via ViewBag but I think I just want to add a partial view that I can add to the master page.
Can someone please tell me how to create a partial view I can add to master page so its used on every page and be able to load the list of links required i.e. by binding IEnumerable model to Partial View?
Try using ChildActionExtensions.Action
In your layout:
#Html.Action("MyAction", "MyController")
Controller:
public ActionResult MyAction()
{
var list = // get your list values
return PartialView("MyViewName", list);
}
Then just create your partial view:
#model IEnumerable<WhateverType>
#* View goodness *#
You can use this to bind whatever model you need to your partial view and if you use the Action helper in your Layout.cshtml it'll be rendered on every page.

Web razor grid in mvc View only works with the type IEnumerable?

Good afternoon ...
I'm trying to implement a web grid web grid and call this the partial, but when called and
of a type error sending partial and error type required .. I saw that I looked at the web partial grid is the model that I mandaondo IEnumerable and not IEnumerable ... have some form of change that make the grid work with a web page not IEnumerable?
and another thing when I call a partial view has to be the same type of view?
to call gives a view of another directory?
If you're asking if the view holding your WebGrid needs to be strongly typed with IEnumerable<> then the answer is no. You can pass your collection to WebGrid through ViewBag :
controller :
List<MyObject> myList = new List<MyObject>();
// do something with the list
ViewBag.MyCollection = myList;
view:
#{
var grid = new WebGrid(ViewBag.MyCollection);
}
But if you're asking wether WebGrid requires IEnumerable - yes it does, You need to pass a IEnumerable interface implementing object (List<>, Linq result etc. )
To call a partial cross-controllers you can easily do :
#Html.Partial("~/Views/Controller/View", model)
take a look at Calling #Html.Partial to display a partial view belonging to a different controller
I really hope that I guessed right about your question ...

Passing parameters in partial Views - MVC3/Razor

How can I pass parameters to a partial view in MVC3 (razor). I replaced a regular View page with a Partial View in my MVC project. For a regular View page, I passed parameters like
public ActionResult MeanQ(int id)
{
Access access= db.Access.Find(id);
return View(access);
}
Now since I changed the view to a partial view, I have the following code instead:
public ActionResult MeanQ(int id)
{
Access access= db.Access.Find(id);
return PartialView("_MeanQPartial");
}
but do not know how I can still pass the parameter 'id' to make it work like before. Please help. For what its worth, the View or the partial View , both are triggered by a link and displayed in a Jquery Modal Dialog box.
Try this
return PartialView("PartialViewName", access);
Simply give it as 2nd parameter. PartialView method has 4 overloads and this includes one with two parameters PartialView(string viewName, object model)
public ActionResult MeanQ(int id)
{
Access access= db.Access.Find(id);
return PartialView("_MeanQPartial", access);
}
For what its worth, the View or the partial View , both are triggered by a link and displayed in a Jquery Modal Dialog box.
View would return an entire page using your layout. PartialView only returns the HTML from your partial. For a modal dialog, the partial is enough. No need to retrieve a complete page.

ASP.NET MVC: Reuse of View by two controller actions

'Add' and 'Edit' views are typically more or less identical. How can I reuse a View so that Foos/Add and Foos/Edit/[Id] both use it? What would the actions look like?
Thanks
Simply specify the view name when calling the View() method like
public ViewResult Add() {
//...
return View("Foo");
}
public ViewResult Edit(int id) {
//...
var model = repository.get(id);
return View("Foo", model);
}
Your view will have to handle null/empty model values for the Add action or you could populate your model with default values.
You may want to consider using an Editor Template as opposed to reusing the same View. An Editor Template is a partial View that is used for editing and/or inserting data.
This would require separate views but the code would be minimal. The bulk of the code would be in the template which you would reuse for both the Add and Edit actions.
After you create your template, your Add View would look like (Razor):
#model Models.Foo
<h2>Add</h2>
<p>
#Html.EditorFor(model => model) // equivalent to EditorForModel()
</p>
And your Edit View would look like:
#model Models.Foo
<h2>Edit</h2>
<p>
#Html.EditorFor(model => model) // equivalent to EditorForModel()
</p>
I would use jQuery ajax. Clicking on Add or Edit would call serve action which will return the same PartialView (if you want to reuse it).
Then, in the success function of ajax call, you have just to put returned html (from that PartialView) into certain part of your page (or popup).
Nice and clean and no page reload...

Resources