How to add Custom Controls in razor view in mvc 4 - asp.net-mvc-3

How to add Custom Controls in razor view in mvc 4
There is a project requirement to add custom control in razor view in mvc4 but i am unable to do it. So need help
Is there any possibility to add custom controls in razor view mvc4. If yes then how is it possible

I'm not sure what you meant by controls but you could use Html Helpers:
#helper ProductListing(List<Product> products) {
<ul id="products">
#foreach(var p in products) {
<li>#p.Name ($#p.Price)</li>
}
</ul>
}
<div>
#ProductListing(Model.Products)
</div>
See this post written by Scott Gu: http://weblogs.asp.net/scottgu/archive/2010/07/02/introducing-razor.aspx

Related

multiple partial views and one submit button in a layout view

Im not sure if this is the right approach to do this. In my searchs only found multiple partial views and one submit button but not in a layout.
I have tree different views each with their own model with the same _ViewStart.cshtml -> _Layout.cshtml (MVC convention). This is the control for the first one:
[Authorize]
public ActionResult UpDateData1 ()
{
return View();
}
[HttpPost]
public ActionResult UpDateData1(Data1Model model)
{
if (ModelState.IsValid)
{
SOME CODE…
}
else
{
ModelState.AddModelError…
}
return View(model);
The 3 views are pretty generics, but I don't want a submit button in there.
Also in _Layout.cshtml I have a partial view with common validation code for the 3 views:
<section id="main">
#RenderBody()
<div>
#Html.Partial("_CommonValidation ", new store.Models.CommonValidation())
</div>
<div>
<input type="submit" value="Only one Button" />
</div>
<div>
#Html.ActionLink("BackTo…", "MyAccountConfig", "Account")
</div>
</section>
I don’t want a submit button in _CommonValidation view neither.
It is possible to use "ONLY ONE BUTTON” submit button in the Layout.cshtml to validate the models of the partial _CommonValidation and the RenderBody() views ? it is a good practice to include strongly typed partial views in the _Layout.cshtml view? I'm new to MVC 3 so I don’t have any idea what direction to take… JavaScripts, HTML Helpers maybe...
Thanks

Dynamically Add Rows when using Editor Templates and EditorFor in MVC 3

Expanding on a previous question, I'm using EditorFor to edit a list of objects of type (let's say) Hobby.
Now, I need to implement functionality to add editors for individual Hobby objects, so that the user can add additional hobbies.
I read Steven Anderson's blog about how to do that in MVC 2. However, he's rendering the individual Hobby instances using a loop and RenderPartial, rather than using EditorFor. I understand his approach of making Ajax calls to render another partial view and inserting the view result into the DOM, but don't know how to apply the Ajax add in my case, with EditorFor.
His code:
<% foreach (var item in Model)
Html.RenderPartial("GiftEditorRow", item);
%>
My code is using EditorFor like this:
// Model is a List<Hobby>
#Html.EditorFor(model => model.AllowedSurveys)
How do I add an additional Hobby editor, given that the editor is implemented as an Editor Template rather than as a Partial View?
You could replace your editor template with a partial:
#foreach (var item in Model.AllowedSurveys)
{
Html.RenderPartial("_Hobby", item);
}
and inside the partial (~/Views/controllerName/_Hobby.cshtml):
#model Hobby
<div class="editorRow">
#using(Html.BeginCollectionItem("AllowedSurveys"))
{
#Html.EditorFor(x => x.Name)
...
}
</div>
The Html.BeginCollectionItem custom helper is used to generate names with Guids and allow for reordering and adding new items to the collection using javascript. You can download it from the updated article. The one you were reading was for ASP.NET MVC 1 and is obsolete.

ASP.NET MVC Parial views and layouts in Razor

Is it possible for a partial view to have a layout so that whenever the partial is rendered - it is rendered with its own layout ?
Yes you can have a layout for a partial, just make sure your layout is not defining the header and the body sections of your html (because it's for a partial).
here is a sample:
_partialViewLayout.cshtml
<div class="partial-view-container">
#RenderSection("partialViewContent", true)
</div>
partialView.cshtml
#{
Layout = "~/_partialViewLayout.cshtml";
}
#section partialViewContent {
<div></div>
}

How can I get .cshtml from Database dynamically

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

MVC 3 Rendering 2-Level Menu as Partial View

I would like to implement a 2-level parent/child menu in my MVC 3 site such as
Company
- Background
- Contact
I have implemented a single, parent level menu as a PartialView like so ...
<div id="menu" class="block">
<ul id="menuItems">
foreach (var item in Model)
{
<li id="#item.Id">#Html.ActionLink(item.Name, item.Action,item.Controller)</li>
}
</ul>
</div>
and then included it on my MasterPage ...
#{Html.RenderAction("MainMenu", "Menu");}
The problem is that I would like to render a second child-menu based on the menu item selected at the parent level. This involves passing the Id of the parent into the controller action that returns the menu model. I'm not sure how I can pass this parent Id into the controller action. Can anyone provide any insights into this? I'm using MVC3 & Razor.
You may want to check out MvcSiteMapProvider which handles multilevel menu and sitemaps.
Looks like you're using Razor and while I'm not super familiar with that I'll take a shot at it. Basically you're going to pass a new object that has a single property of "id" to the MainMenu view. That will create another menu. Your MainMenu action should take an optional parameter of id.
public ActionResult MainMenu(int? id = null) {
...
}
Here is what your new list item would look like.
<li id="#item.Id">#Html.ActionLink(item.Name, item.Action,item.Controller)
#{Html.RenderAction("MainMenu", "Menu", new { id = item.Id });}
</li>

Resources