How Can I Have View-Specific contents Using Asp.Net MVC 3 and Razor?

The equivalent of content placeholders in Razor are sections. In your _Layout.cshtml: <head> @RenderSection(“Styles”, required: false) </head> Then in your content page: @section Styles { <link href=”https://stackoverflow.com/questions/4739907/@Url.Content(“~/Content/StandardSize.css”)” /> } An alternative solution would be to put your styles into ViewBag/ViewData: In your _Layout.cshtml: <head> @foreach(string style in ViewBag.Styles ?? new string[0]) { <link href=”https://stackoverflow.com/questions/4739907/@Url.Content(style)” /> … Read more

Attaching and detaching entities from context correctly in EF4.1

IEntityWithKey is interface for other types of entities. It is for “big” entities. For example EntityObject implement this interface. These entities are not considered as POCO and are not supported by DbContext API. If you want to use IEntityWithKey your classes must implement it – it is not something that would happen automatically. Correct attaching … Read more

Propagating QueryString parameter in RedirectToAction calls

public class PreserveQueryStringAttribute : ActionFilterAttribute { public override void OnActionExecuted(ActionExecutedContext filterContext) { var redirectResult = filterContext.Result as RedirectToRouteResult; if (redirectResult == null) { return; } var query = filterContext.HttpContext.Request.QueryString; // Remark: here you could decide if you want to propagate all // query string values or a particular one. In my example I am // … Read more

.Net Mvc 3 Trigger (other than submit button) Unobtrusive Validation

$(‘form’).valid() should work. Let’s exemplify. Model: public class MyViewModel { [Required] public string Foo { get; set; } } Controller: public class HomeController : Controller { public ActionResult Index() { return View(new MyViewModel()); } } View: @model MyViewModel <script src=”https://stackoverflow.com/questions/6301492/@Url.Content(“~/Scripts/jquery.validate.js”)” type=”text/javascript”></script> <script src=”https://stackoverflow.com/questions/6301492/@Url.Content(“~/Scripts/jquery.validate.unobtrusive.js”)” type=”text/javascript”></script> @using (Html.BeginForm()) { @Html.LabelFor(x => x.Foo) @Html.EditorFor(x => x.Foo) @Html.ValidationMessageFor(x => … Read more

redirect to return url after login

I use a combination of the above suggestion and Request.UrlReferrer to get the previous location: public ActionResult LogOn(string returnUrl) { //So that the user can be referred back to where they were when they click logon if (string.IsNullOrEmpty(returnUrl) && Request.UrlReferrer != null) returnUrl = Server.UrlEncode(Request.UrlReferrer.PathAndQuery); if (Url.IsLocalUrl(returnUrl) && !string.IsNullOrEmpty(returnUrl)) { ViewBag.ReturnURL = returnUrl; } return … Read more

Image display on MVC view

You have specified an absolute path which doesn’t exist on the client computer. Try like this: <img src= “https://stackoverflow.com/questions/5248273/@Url.Content(“~/uploads/FileUpload12011_03_02_11_49_22.jpg”)” alt=”IMAGES” /> or if your model variable contains “~/uploads/FileUpload12011_03_02_11_49_22.jpg” you could: <img src= “https://stackoverflow.com/questions/5248273/@Url.Content(Model.PictureLocation)” alt=”IMAGES” />

Conditionally change CSS class in Razor view

@{ int counter=0; } @foreach (var item in Model) { counter++; <div class=”@(counter<=3 ? “classRed”:”classBlue”)”> <img src=”https://stackoverflow.com/questions/11785166/@item.Blog.Image.img_path” alt=”Not Found” /> //other markup also here </div> if (counter == 6) { counter = 0; } } Where classRed and classBlue are the CSS classes

MVC 3 Layout Page, Razor Template, and DropdownList

As usual you could start by defining a view model: public class YearsViewModel { public string Year { get; set; } public IEnumerable<SelectListItem> Years { get { return new SelectList( Enumerable.Range(1900, 112) .OrderByDescending(year => year) .Select(year => new SelectListItem { Value = year.ToString(), Text = year.ToString() } ), “Value”, “Text”); } } } Then a … Read more