I'm using MVC 5, and have a page where I will list out payments for a contract. there are times when there are no payments associated with a contract. my view brings in the model to list:
#model List<MyProject.Models.PaymentViewModel>
the Action Method on the Payment view looks like:
[HttpGet]
public ActionResult Payment(int id)
{
var payments = contractController.GetPaymentsForActiveContract(id);
return View(payments);
}
Now GetPaymentsForActiveContract returns a List and this works fine when I use
#Html.Partial("partialviews/_Payments")
on my view. but what i'm trying to do now is have another action method get called, which will return either a view which will display a table of payments, or another one which will write out there are none if no payments exists.
I have that action defined as:
public ActionResult GetPayments(List<PaymentViewModel> payments)
{
if (payments.Count > 0)
{
return PartialView("partialviews/_Payments", payments);
}
return PartialView("partialviews/_NoPayments");
}
I'm not sure how to pass the Model into this Action from my view. I'm looking through the #Html methods, but can't seem to find anything which would allow me to call the GetPayments method passing in the model and having it return either Partial View.
If you want to call controller action from view, you can use
#Html.Action()
method and pass payments list as route value
#{
var paymentsList = new List<PaymentViewModel> {new PaymentViewModel(), new PaymentViewModel()};
}
#Html.Action("GetPayments", new { payments = paymentsList })
Related
I have a partial view in my MVC app which I load into a container dom element. I do this by first calling the controller, like so:
$(container).load('/xxx/GetPartialView');
In the controller I return the partial view:
public PartialViewResult GetPartialView()
{
return PartialView("SomePartial", null);
}
This works just fine. However, I would like to send a parameter (just a simple string value) along from the controller to the partial view I'm creating. This I understand, can be done by the use of a model, like for example:
public PartialViewResult GetPartialView(string someValue)
{
return PartialView("SomePartial", new SomeDummyModel(someValue));
}
But I would like to avoid the model instance if possible, as it seems like a lot of overhead. I want to just send the string value as a parameter. Is that possible?
Instead of passing a custom class such as SomeDummyModel you can simply pass someValue. Assuming that someValue is string from your explanation, that would mean you would accept string in the #model of your partialView.
controller
public PartialViewResult GetPartialView(string someValue)
{
return PartialView("SomePartial", someValue);
}
partial
#model string
<div>Hello, #Model :)</div>
You can also use the ViewData object to pass simple items like that.
public PartialViewResult GetPartialView()
{
ViewData["someValue"] = "hello";
return PartialView("SomePartial", null);
}
And then in the view access it:
<div>#ViewData["someValue"].ToString() :)</div>
This works without a model.
You can put pretty much anything into the ViewData object, you just need to cast it out
Partial view does not return any data.When i check with debug tool on PartialView page(_ContentBlock.cshtml), model seem to null.
Controller
public ActionResult Module()
{
int RouteDataValue = default(int);
if (RouteData.Values["id"] != null)
{
RouteDataValue = int.Parse(RouteData.Values["id"].ToString());
}
using (Models.ContentModel db = new Models.ContentModel())
{
var Query = from n in db.PageModule
join m in db.Module on n.ModuleId equals m.ModuleId
where n.PageId == RouteDataValue
select m.PhysicalPath;
return PartialView(Query.Single()); //returns PartialView such as ~/Modules/Content/_ContentBlock.cshtml
}
}
public PartialViewResult ContentBlock()
{
using (Models.ContentModel db = new Models.ContentModel())
{
return PartialView("~/Modules/Content/_ContentBlock.cshtml", db.ContentBlock.Where(n => n.PageId == 2).Single());
}
}
Page.cshtml
#Html.Action("Module")
_ContentBlock.cshtml
#model IEnumerable<Models.ContentBlock>
#foreach (var item in Model)
{
#Html.DisplayFor(n => item.Content)
}
You seem to have used the Html.Partial helper instead of Html.Action. So you were basically only rendering the partial without ever hitting the controller action that is supposed to populate and the model to the partial.
Your page Page.cshtml is calling the partial view action Module using:
#Html.Action("Module")
The action called Module is being executed. In that action, your query results in a path to your view, such as:
"~/Modules/Content/_ContentBlock.cshtml"
That action is returning the single result of that query using:
return PartialView(Query.Single());
What this is doing is passing the name of the view to the PartialView method to return which view is going to be used to display data from the action. In addition, no model data is included in this return.
That's where your problem is. When you return the path to the partial view, you are simply telling the MVC system what view to use to display the data from Module. It won't actually call another partial view. That's not how it works. So your model is null because, you didn't pass any data in your PartialView(...) call.
You have another action called ContentBlock. But that action is not being called because nothing is calling it.
Edit:
Another problem you have is that _ContentBlock.cshtml uses a model of IEnumerable<ContentBlock>, but you're only passing it a .Single() from your ContentBlock action.
In my project I have a controller that allows you to create multiple letters of different types. All of these letter types are stored in the database, but each letter type has different required fields and different views.
Right now I have a route set up for the following URL: /Letters/Create/{LetterType}. I currently have this mapped to the following controller action:
public ActionResult Create(string LetterType)
{
var model = new SpecificLetterModel();
return View(model);
}
I also have a View called Create.cshtml and an EditorTemplate for my specific letter type. This all works fine right now because I have only implemented one Letter Type. Now I need to go ahead and add the rest but the way I have my action set up it is tied to the specific letter type that I implemented.
Since each Letter has its own model, its own set of validations, and its own view, what is the best way to implement these actions? Since adding new letter types requires coding for the model/validations and creating a view, does it make more sense to have individual controller actions:
public ActionResult CreateABC(ABCLetterModel model);
public ActionResult CreateXYZ(XYZLetterModel model);
Or is there a way I can have a single controller action and easily return the correct model/view?
You can do one of the following:
Have a different action method for each input. This is because the mvc framework will see the input of the action method, and use the default model binder to easily bind the properties of that type. You could then have a common private method that will do the processing, and return the view.
Assuming XYZLetterModel and ABCLetterModel are subclasses of some base model, your controller code could look like:
public class SomeController : Controller
{
private ISomeService _SomeService;
public SomeController(ISomeService someService)
{
_SomeService = someService;
}
public ViewResult CreateABC(ABCLetterModel abcLetterModel)
{
// this action method exists to allow data binding to figure out the model type easily
return PostToServiceAndReturnView(abcLetterModel);
}
public ViewResult CreateXYZ(XYZLetterModel xyzLetterModel)
{
// this action method exists to allow data binding to figure out the model type easily
return PostToServiceAndReturnView(xyzLetterModel);
}
private ViewResult PostToServiceAndReturnView(BaseLetterModel model)
{
if (ModelState.IsValid)
{
// do conversion here to service input
ServiceInput serviceInput = ToServiceInput(model);
_SomeService.Create(serviceInput);
return View("Success");
}
else
{
return View("Create", model);
}
}
}
The View code could look like:
#model BaseLetterModel
#if (Model is ABCLetterModel)
{
using (Html.BeginForm("CreateABC", "Some"))
{
#Html.EditorForModel("ABCLetter")
}
}
else if (Model is XYZLetterModel)
{
using (Html.BeginForm("CreateXYZ", "Some"))
{
#Html.EditorForModel("XYZLetter")
}
}
You would still have an editor template for each model type.
Another option is to have a custom model binder that figures out the type, based on some value in a hidden field, and then serializes it using that type.
The first approach is much more preferred because the default model binder works well out of the box, and it's a lot of maintenance to build custom model binders.
I have the controllers:
Controller A
{
public ActionResult ExecuteSomeStuff()
{
....use TempData
returns a View("StuffMade", SomeModel);
}
}
and
Controller B
{
public ActionResult DoStuff()
{
...fill up TempData
returns RedirectToAction("ExecuteSomeStuff", "A");
}
}
The problem is that the ExecuteSomeStuff method on controller A is executed twice.
I dont need the actual redirect to be made, I just want the result(the view) from the ExecuteSomeStuff method to be returned for DoStuff method.
I dont want to have a reference to controller A in controller B in order to call the method directly.
Is there any way to do this without the physical redirect or new reference to be made to controller A in controller B??
If I'm understanding you correctly, instead of a RedirectToAction, you can return the ExecuteSomeStuff View from Controller B. You will need to be able to set the Model to pass into the View from Controller B. So you may still need to make some reference to the methods used in Controller A.
Controller B:
{
public ActionResult DoStuff()
{
...fill up TempData
//set the model then Return the View with the Model passed in
return View("ExecuteSomeStuff","A",SomeModel);
}
UPDATE
This will look for a View with the same name as the action ExecuteSomeStuff without actually entering the ExecuteSomeStuff action. If your View has a different name, you can explicity specify the View to return like this:
return View("../A/theCorrectView.aspx", model);
I am writing an application wherein I need to send a System.Collections.ArrayList data as a parameter from one controller action to another.
I am using
return RedirectToAction("action1","controller1", new { arrList = arrListInFirstAction});
But since the ArrayList goes out of scope in the first action, the parameter in the redirected to action receives a null parameter.
Can someone please help me find an answer to this problem.
Thanks.
you can not send complex types as route parameters. you can, however, use TempData collection to keep that object for one request and on next request it will be automatically removed from collection
publci ActionResutl action()
{
TempData["arr"] = new int[]{1,2,3};
return RedirectToAction("action1");
}
Public ActionResult action1()
{
int[] arr = TempData["arr"];
return View();
}