Umbraco 5 - passing data from controller to view - model-view-controller

I have been playing around with Umbraco 5 for some days now. I have made a partial view with some dummy-text that I have inserted into a page template. Work's fine. The problem is when I have to pass data from a controller to the view.
The view inherit from RenderViewPage (#inherits RenderViewPage) as default in Umbraco 5. I tried to do it the regular MVC way by #model ViewPage<Umbraco.Cms.Web.UI.Models.Test> but I got an error.

You should start by creating a Surface controller (can be done in seperate project or create controller folder directly in main project):
public class ContactFormSurfaceController : SurfaceController
{
[ChildActionOnly]
public PartialViewResult ContactForm()
{
var model = new ContactViewModel();
return PartialView(model);
}
}
Don't inherit form RenderViewPage just strongly type your view with your own model
Then create a macro that call the ChildAction ContactForm
You can add your action through editor or via code in templates: #Umbraco.RenderMacro("ContactForm")

Related

How to Access PartialView Result in Master Page in mvc3 using razor

I have a method in controller like this
public PartialViewResult CabinSearch()
{
return PartialView();
}
and I have created a view to this method.
I need to access this partial view to master page
In Razor, the #Html.Partial(viewName) helper will render the named partial view inline in the view. There are other invocations of that to pass a model object to the partial view if it is required.
If you need to invoke the controller method to perform some calculation or database access (as you imply) you can instead use #Html.Action(actionName, controllerName) or some variant of that - where CabinSearch is the action name in your case. This will invoke the controller, which will do whatever it does (model preparation) and then render the partial view, which will then be inlined into the page.

How can we include a form in _layout?

I currently have a _layout.cshtml used by every page of my website.
I need to put a form on each page displayed as a popin.
So, i created a new PartialView (the content of my form) with its corresponding ViewModel and called it in _layout.cshtml.
However, i have a model conflict between ViewModels of pages using the layout and the ViewModel used by the new form (since we can't have directly two models for the same view).
The model item passed into the dictionary is of type 'XXX', but this
dictionary requires a model item of type 'YYY'.
How can we include a form in _layout without this conflict ?
The following has worked for me with a sidebar on every page.
Create a controller for your partial view
In that controller, create a method for the view you want to return, and be sure to use the [ChildActionOnly] filter
public class PartialController : Controller
{
[ChildActionOnly]
public PartialViewResult Alerts()
{
return PartialView("Alerts", messages);
}
}
In your _layout view, you'll have the following:
#Html.Action("Alerts", "Partial")
(instead of #Html.RenderPartial or #Html.Partial)
It sounds like you already have what you need for the view.
I have not used this with a form, but it should work similarly. Hope this helps.

strange behavior of MVC3

I created a custom Model i.e. to support my Razor View. Then I created a controller as following`namespace MyCandidate.Controllers
public class CandidateViewModelController : Controller
{
//
// GET: /CandidateViewModel/
public ActionResult Index()
{
return View();
}
}
I also have the following statement in my _Layout.cshtml
#Html.ActionLink("Canid", "Index", "CandidateViewModel")
Next i created a view and the very first statement of the view is
#model MyCandidate.Models.CandidateViewModel
when i run my project i get the following error
The view 'Index' or its master was not found or no view engine supports the searched locations. The following locations were searched:
I spent more than 3 hours but could not figure out?
your Index() not get any parameters, but you send "CandidateViewModel") add Index(string input) method with attribute [HttpGet] to controller.
this error meen you haven't view "Index" in "Views/CandidateViewModel/Index.cshtml".
Maybe you delete master page files (_ViewSrat, _Layout)
Or you made a mistake when you change your routes
Change your ActionLink to:
#Html.ActionLink("Canid", "Index")
If you want to pass any data to View , you can also use ViewBag:
// Controller :
ViewBag.CandidateValues = CandidateViewModelData;
// View
#Html.Label("LabelName", (CandidateViewModel) ViewBag.CandidateValues.FiledName);

Using two partial view in MVC3

In my asp.net mvc3 application i have created two partial views for two different action that is,
partialviewresult setcomment and
partialviewresult getcomment
i have created partial view using create a strongly type view and different scaffold template
for _setcomment i am using create template and for _getcomment i am using List template.
Now i want to call both _setcomment and _getcomment partial view in one view.
in my view file .cshtml
_setcomment -
#model <NAMESPACE>.<MODELNAME>
<some code>
_getcomment -
#model IEnumerable<<NAMESPACE>.<MODELNAME>>
<some code>
how can i call diiferent partial view in one view?
any suggestions?
There are different ways to do it.
If you already have the model class data in the Main view you can use like
In the main view call
#Html.Partial("PartialViewName1",model1)
#Html.Partial("PartialViewName1",model2)
If you do not have the model class data in the mail view then you can call the action on the controller and from there return the partial view.
#Html.Action("Controller","Action1")
#Html.Action("Controller","Action2")
In the Controller class
PartialResult Action1()
{
model = new ModelClass();
return PartialView(model);
}
Hope this helps.
The answer to your question is to use the following within a single view:
#{ Html.RenderAction("ActionName", "ControlerName"); }
#{ Html.RenderAction("ActionName2", "ControlerName2"); }
This would do what you are trying to achieve, however, I think there is a problem with design. What are you trying to achieve?

Calling another Controller post method

I am using ASP.NET MVC 3 for my website.
I have created a partial view that has "Previous, Next and Save" buttons. I am calling this partial view on my master page.
My requirement is that on what ever View I am I must be able to call different Save methods in different controllers by passing respective Model data to controller actions.
Example
I have 4 step data input, I have a different controller for each step.
If i am on step 1 and i click Save the form Values should go to Step1Controller's action method,
If I am on step 2 then post should call Step2Controller
Something like this this:
public ActionResult Save(GenericModel model)
{
//use reflection to find out model type
//call appropriate controller action with model
return RedirectToAction("Create", new { Controller = "Conference", Action = "Create" });
}
This save method will be called for Save button on the Master page. How can I achieve this?
Are the forms on separate actions on the controllers?
If so, just set the form action on each page to point to the relevant controller. So form 1 is
<form method="post" action="/step1controller/action">
Form 2 is:
<form method="post" action="/step2controller/action">
Does that solve your problem?
I would create a private method in the controller and call in every part of the first steps.
private bool Save(GenericModel model)
{
......
}
[HttpPost]
public bool SaveStep1(GenericModel model)
{
this.Save(model);
}
[HttpPost]
public bool SaveStep2(GenericModel model)
{
this.Save(model);
}

Resources