I have a controller that looks like this:
[RoutePrefix("items")]
public partial class ItemsController : Controller
{
public ActionResult Index()
{
var model = new MyApp.Models.Items();
return View(model);
}
// POST: /items/delete
[HttpPost]
[Route("delete")]
public ActionResult Delete(DeleteItemModel model)
{
// delete the item
model.Delete();
return RedirectToAction("Index");
}
}
In my Index.cshtml, I have the following:
#model MyApp.Models.Items
<h2>Hello</h2>
<form action="#Url.Action(MVC.Items.Delete())" method="post">
<button type="submit">delete</button>
</form>
The above is clearly a subset of my view. My main concern is, how to pass a DeleteItemModel from my form back to the Delete action. What am I missing? The MVC.Items.Delete path was generated from T4MVC. Yet, I can't figure out how to pass a model into it. Is there a fix here, or do I have to use some other approach entirely?
Related
I am thinking about a scenario rather similar to this question asked some time ago: How to call another controller Action From a controller in Mvc
In the scenario I am thinking of, controller B would however return a partial view that I would like to incorporate into the ViewResult returned by the Home controller, so the code might look like this:
public class Home
{
public ActionResult Index()
{
ActionResult partialViewFromCtrl_B = RedirectToAction(actionName: "SomeAction",
controllerName: "Controller_B");
// The returned view should include the partial view `partialViewFromCtrl_B`
return View();
}
}
public class Controller_B
{
public ActionResult SomeAction()
{
// Do some stuff
return PartialView(viewName: "_SomeAction", model: someModel);
}
}
Is there a way to combine the partial view returned from controller B into the view returned from the Home controller?
If possible, I would like to keep the logic and the views defined for controller B separate from the Home controller rather than merging everything into the Home controller.
Your help is very much appreciated!
Edit:
One possible solution to my scenario might by if the Index.cshtml of the Home controller looks like this
#model SomeModelType
...
#Html.Partial(partialview: "~/Controller_B/_SomeAction", model: Model)
...
and the Index() action of the Home controller would look like this:
public ActionResult Index()
{
// Get the model from Controller_B instead of the partial view
SomeModelType someModelFromCtrl_B = new Controller_B().returnSomeModel();
return View(someModelFromCtrl_B);
}
This however does not seem to be the best approach to take here for me.
So if possible I would like to combine the partial view Controller_B/_SomeAction and the Index view of the Home controller in the action method of the Home controller if that is possible.
Through the code posted I can not understand what you want to return.
based on your little code I think you intend to return Index values in PartialView.
Take a look at the code below:
public class Home
{
public ActionResult Index()
{
if(//cond){
// Do some stuff
return PartialView(viewName: "_SomeAction", model: someModel);
}
else{
ActionResult partialViewFromCtrl_B = RedirectToAction(actionName: "SomeAction",
controllerName: "Controller_B");
// The returned view should include the partial view `partialViewFromCtrl_B`
return View("Index");
}
}
}
I have a .NET Core 2.1 MVC application.
I have a Home controller that looks like this:
public class HomeController : Controller
{
public IActionResult Index()
{
return View();
}
public IActionResult CountView(MyModel model)
{
...
return PartialView(model);
}
}
In the Index view I want to display a patial view called CountView.
But when I use the following method in the Index.cshtml:
#await Html.PartialAsync("CountView", Model)
the view is displayed as I wanted to but the method in the controller never gets called and thus the partial view doesn't get the data it needs that is intended to be fetched by the controller method.
Why is that so?
And how can I make it to work as desired?
#await Html.PartialAsync("CountView", Model)
Renders a Partial View, it doesn't call any Controller for that. The correct way for doing this in ASP.NET Core is to create a View Component.
As for the implementation, here's a simple one modified from the official documentation previously linked:
Create a ViewComponents folder at the root of the project
Inside it, create a ``CounterViewComponent` class:
public class CounterViewComponent: ViewComponent
{
public async Task<IViewComponentResult> InvokeAsync(MyModel model)
{
// magic here
return View(model);
}
}
Call it in your view:
#await Component.InvokeAsync("Counter", new { model = Model })
I am new to this MVC Architecture so please execuse for basic or amature questions.
I need to implement a search functionality. I created an action in Home Controller SearchFNW(searchstring). In this method I got a list of ViewModels with searched data.
I am passing this list to Index.cshtml and trying to display results in a table.
I am able to get the View Model's List data to View.
Index.cshtml
#model List<First_Niagara_Works.ViewModels.AccountViewModel>
#{
#using (Html.BeginForm("Index", "Home"))
{
if (Model != null)
{
// code to populate table using model
}
}
}
Homecontroller.cs
public ActionResult SearchFNW(string FNWtodo, string SearchInput)
{
// all code to load viewmodels to list
return View(#"~/Views/Home/Index.cshtml", lstAccountVM);
}
public ActionResult Index()
{
return View();
}
Problem: After execution of this code it redirects to Layout.cshtml and from there it calls Index action again .. and started all over again with #Html.beginform(). so not able to see table data with searched results..
try:
[HttpPost]
public ActionResult Index(string FNWtodo, string SearchInput)
{
// all code to load viewmodels to list
return View(lstAccountVM);
}
public ActionResult Index()
{
return View();
}
Your form #using (Html.BeginForm("Index", "Home")) submits a POST back to the Index Action in the controller. You just need to use the Index action that captures the [HttpPost] and it will return the Index view but with your populated model.
I want to use 2 models. The first is on Index.cshtml page, and the second is on _Layout.cshtml page
In the controller which contains the action public ActionResult Index(){...}, I declare some values and return it to View(). Like this:
public ActionResult Index()
{
HomePageViewModel model = new HomePageViewModel();
// do something...
return View(model);
}
And in MyProjectName.Models, I write some classes to check login account and put it on the page _Layout.cshtml. Like this:
On page _Layout.cshtml:
#using MyProjectName.Models
#model MyProjectName.Models.LoginModel
#if (Model.LoginAccount != null)
{
foreach(Account acc in Model.LoginAccount)
{
#Html.ActionLink(#acc.Email, "SomeAction", "SomeController", null, new { id = "loginEmail" })
#Html.ActionLink("Logout", "SomeAction", "SomeController", null, new { id = "logout" })
}
}
The code on page _Layout.cshtml doesn't work. It said that: I have returned a model (HomePageViewModel model), but some values which I want to render is referenced from MyProjectName.Models.LoginModel
Main requirement is: the first model is used to display the content on page Index.cshtml, and the second model is used to check user login (on page _Layout.cshtml).
Can you tell me how to do that? Thank you!
In your layout use Html.Action() or Html.RenderAction() to call a ChildActionOnly method that returns a partial view for LoginModel
[ChildActionOnly]
public ActionResult Login()
{
LoginModel model = // initialize the model you want to display in the Layout
return PartialView(model);
}
and create a partial view that displays the links, then in the Layout
# { Html.RenderAction("Login", "yourControllerName") }
A better approach would be to use partial views and the ViewBag.
in your controller you would do something similar to this:
public class HomeController : Controller
{
public ActionResult Index()
{
ViewBag.Accounts = new AccountsViewModel();
ViewBag.HomePage = new HomePageViewModel();
return View();
}
}
From here you'd pass your model from the ViewBag to a partial view
#{
AccountViewModel Accounts = (AccountViewModel)ViewBag.Accounts;
}
#Html.Partial("_accountPartial", Accounts)
I have a mvc view with a partial view.There is a ActionResult method in the controller which will return a PartialView. So, I need to pass ViewBag data from that ActionResult method to Partial View.
This is my Controller
public class PropertyController : BaseController
{
public ActionResult Index()
{
return View();
}
public ActionResult Step1()
{
ViewBag.Hello = "Hello";
return PartialView();
}
}
In Index.cshtml View
#Html.Partial("Step1")
Step1.cshtml partial view
#ViewBag.Hello
But this is not working. So, what is the correct way to get data from viewbag.
I think I'm following wrong method. Please guide me.
You can use it as mentioned below :
In your View :
#Html.Partial("[ViewName]", (string)ViewBag.Message)
And Your partial View :
#model String
<b>#Model</b>
As Shown Above ViewBag.Message will be passed to the partial view. and in your partial view you can use it as a #Model.
Note : here type of ViewBag.Message is string. You can pass any type.
If you don't have to use ViewBag, you can use TempData. TempData is shared for the whole execution chain.
public class PropertyController : BaseController
{
public ActionResult Index()
{
return View();
}
public ActionResult Step1()
{
TempData["Hello"] = "Hello";
return PartialView();
}
}
In Index.cshtml View
#Html.Partial("Step1")
Step1.cshtml partial view
#TempData["Hello"]
"Child actions follow a different controller/model/view lifecycle than parent actions. As a result they do not share ViewData/ViewBag."
The answer provides an alternate way of passing data.
Does a child action share the same ViewBag with its "parents" action?
Old question but if anyone here to find solution for this question..
You can pass viewbag value to partial with viewdatadictionary.
In your view:
#Html.Partial("_Partial", "", new ViewDataDictionary { { "permalink", ViewBag.Permalink } })
and in partial view use it like this:
ViewData["permalink"]
You can try this in order to pass ViewBag to partial view from action:
Your controller:
public class PropertyController : Controller
{
public ActionResult Index()
{
return View();
}
public ActionResult Step1()
{
ViewBag.Hello = "Hello";
return PartialView("_Partial1", ViewBag.Hello);
}
}
Your view (Index.cshtml):
#Html.Action("Step1")
Your partial view (_Partial1.cshtml):
#ViewBag.Hello
return PartialView("partialviewname", obj);