How can I get .cshtml from Database dynamically - asp.net-mvc-3

I have to say: This is a different question from these:
http://craftycodeblog.com/2010/05/15/asp-net-mvc-render-partial-view-to-string/
ASP.NET MVC Razor: How to render a Razor Partial View's HTML inside the controller action
http://blog.rocketbase.co.uk/2011/04/asp-net-mvc-virtual-path-provider/
ASP.NET MVC load Razor view from database
I want to provide dynamic .cshtml Content from db when i want.
Example:
City Table,FreeHtml Column
#model City
#Html.CheckBox - #Model.Name - #Html.CustomHelper
How can I write as html helper:
#model City
#Html.RazorRaw(Model.FreeHtml,Model)
or as CustomViewResult
public RazorPartialViewResult CityHtml(City city)
{
return new RazorPartialViewResult(city.FreeHtml,city)
}

I think your answer is here:
Storing ASP.Net MVC Views in the Database
You need to replace the default view engine(s) with a custom one.
Here is an example:
http://weblogs.asp.net/imranbaloch/archive/2011/06/27/view-engine-with-dynamic-view-location.aspx

Related

MVC 3 Partial View and what I need to send to it?

I am wondering about a couple variations of forms and partial forms. The submit is on the parent page and I have varied what I pass to the partial view. I have a parent view with a related HomeViewModel (which has public properties Name and public Person Employee {get;set;}
1.) Scenario 1: The main view has something like the following
#model MasterDetailSample.Models.HomeViewModel
#using (Html.BeginForm()) {
<div>
#{Html.RenderPartial("_PersonView", #Model);}
</div>
<input type="submit" value="Save" />
}
In this scenario I am passing to the partial view _PersonView the entire HomeViewModel. Within _PersonView partial view I have to reference a property of the HomeViewModel i.e. Person object via #model.Employee.Name (in this scenario the submit is on the parent form (not within the partial view))
When I hit submit on the form (POST) in the controller i have to access the property of Employee "Name" via the following model.Employee.Name
This seems to work however notice the following variation scenario 2 (where I only pass to the partial the Employee object)
2.) Scenario 2
In this scenario I only want to send the Employee object to the partial view. Again the begin form and submit is on the parent form.
So from the parent form i have
#{Html.RenderPartial("_MasterView", #Model.Employee);}
and so within the partial view i reference the Name property of the Person object via #Employee.Name Now when I submit the form within the controller the Employee object is not available from the auto model binder. I can access the properties via formcollection but not from the model parameter
i.e.
[HttpPost]
public ActionResult Index(ModelViewModel model) {
**//model.Employee is null!**
return View();
}
Why? (is model.Employee null) I would like my partial view to only accept an object of type Person however after submitting from the parent page, the Employee property is null. On the partial view I am using the following on the #model line
#model MasterDetailSample.Models.Person
I would like the partial to only need a Person object to be sent to it, but I would like the submit on the main form. If i do it this way I can re-use the partial view in a few situations however IF i must send HomeViewModel i have significantly limited how I can use this partial view. So, again, I only want to use Person as the model with the partial view but I need to be able to access the properties when submitted from the parent view.
Can this be done? If yes how?
thx
You have a couple of options:
1) One I recommend -> Dont use partial views, instead use EditorFor and create an editor template for Person. With partial views the context is whatever model you pass into the view, this is why your example (1) works and (2) not. However with editor templates the parent context is taken into consideration with the html helpers and will generate the correct input names. Have a look at Darin Dimitrov's answer to a similar question.
2) Use your second example as is, but change the post action to look something like this:
[HttpPost]
public ActionResult Index(ModelViewModel model) {
TryUpdateModel(model.Employee);
**//model.Employee should now be filled!**
return View();
}
3) Use custom html helpers that accepts prefix for input, see this answer I posted a while back for example code. You could then use this inside your partial view.

HTML markup & ASP.NET MVC3

I have parts of the web page which I would like MVC3 to serve to client pages. Instead of making them static in everysite. I have sites which have reoccurring markup in parts in the page, such as banner, navigation. In some sites I will need to customize the markup a little. So I need to be able to extend if possible.
Can someone please tell me if MVC3 is ideal as a solution to this? I am thinking partial views. Can you inherit & extend partial views?
Thanks
Partial Views can be implemented for your needs. For example, you can use them as follows :
#model string[]
#if((bool)Model[0]) {
<span>I like it to be this way.</span>
} else {
<span>No, I like it to be that way.</span>
}
Inside a view, you can call this partial view as follows :
#Html.Partial("myPartialView", new string[] { "true" })
You can pretty much pass anything to partial view as Model.
If you plan to use those partial views on multiple applications, I encourage you to create Nuget package for those Partial Views and get them into each application through Nuget Package Manager.
Banners, navigation, custom panels etc - all these are the components that you might want to add, combine, remove or update as time goes. Yes, partial views would do the job easily, but in my opinion, inheritance isn't a way forward.
Your partial views should be based on view models. For example: OrderPanelViewModel, UserMenuViewModel, CustomPanelViewModel. Each partial view is now a component, a feature that you can add,remove, modify as you feel suited.
What happens if you want to combine banner and a user menu? It's not going to work if they inherit from the same view model, but it will work if they have their own view models.
For example:
public class MyCustomPaneViewModel{
public UserMenuViewModel UserMenu { get; set; }
public UserBannerViewModel UserBanner { get; set; }
}
You can now base your view on this view model
#model YourDll.WebUI.Models.MyCustomPaneViewModel
#section main_content{
#Html.DisplayFor(model => model.UserMenu, "UserMenu")
#Html.DisplayFor(model => model.UserBanner, "UserBanner")
}

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?

Loading multiple controls in ASP.NET MVC

I am creating my first site in asp.net MVC and I have a very beginner question in my mind. i have seen that in controller we are returning the actionview for what we want to display in the page [Most of the example in the websites I can see they are only displaying the content in the page] . What if I have to load 3 drop down list, 2 tables , 2 radio buttons etc. What is the best practice and the correct way to load these many controls on the page?
Chris
It sounds like you are expecting to use controls like one does in ASP.Net Web Forms. However, with MVC the View consists of standard HTML. The controls you mention can just be input select and so on. There are various helper classes and methods that you can use in the view to help you render the HTML you need - In particular take a look at the Razor syntax.
I'd start with looking at a couple of examples, and it should be clearer....
Here's a good one: http://www.nerddinner.com/ (source code here http://nerddinner.codeplex.com/)
Maybe pick up a couple of books from Amazon as well.
HTH
Phil
The examples you typically see use MVC's scaffolding, which creates a very simple Controller/Actions/Views to manipulate a certain Model class. But you're free to show anything you want in your pages. Here's an example on how to show a drop down list.
First create an object that will hold all the stuff you want to display on the page:
public class GameDetailsViewModel
{
public Game Game { get; set; }
public SelectList Players { get; set; }
}
Note the SelectList. It will be used as the source for the DropDownList.
Then the Action fills in this object:
public ViewResult Details(int id)
{
GameDetailsViewModel viewModel = new GameDetailsViewModel();
viewModel.Game = db.Games.Single(g => g.ID == id);
IEnumerable<Player> players = db.Players();
viewModel.Players = new SelectList(players, "ID", "FullName");
return View(viewModel);
}
Note the overload to the View() method, that takes the object we created to package the stuff we need on the page.
Then on the View, you can use an HtmlHelper to render a DropDownList:
#using (Html.BeginForm("signup", "games", FormMethod.Post))
{
#Html.DropDownList("playerID", Model.Players, "Select...", null)
<input type="submit" value="Sign up" />
}
This is a very simple example, but you can extend it to send whatever you want to the View and then render it using plain old HTML or the handy HtmlHelpers.

How can I databind in the _Layout.cshtml

In ASP.NET WebForms, I can use the CodeBehind of a master page to fetch data to use to bind up my navigation.
How can I achieve the same in ASP.NET MVC 3?
Ideally the main navigation would be in the _Layout.cshtml but this file doesn't have it's own model. i.e. It can only use the model supplied by the controller action (assuming a base class and #model directive in the _Layout.cshtml.
Edit
While I realise MVC does not have the notion of DataBinding, I included it here to help describe the functionality I'm looking for.
How can I achieve the same in ASP.NET MVC 3?
The notion of databinding is not common for the MVC pattern. To implement the navigation you could use Html.Action and Html.RenderAction.
Example:
public class NavigationController : Controller
{
public ActionResult Index()
{
NavigationViewModel model = ...
return View(model);
}
}
and then inside the layout:
#Html.Action("Index", "Navigation")
The index.cshtml could be a partial which implements the navigation.

Resources