Showing posts with label Razor. Show all posts
Showing posts with label Razor. Show all posts

Wednesday, September 24, 2014

ASP.NET MVC Razor Unrequired property keeps getting data-val-required attribute

If you have the problem that your text field validation gets generated althoug there is no Required attribute set on property, use the next code in the Application_Start to force the compiler to validate only if there is a required attribute on the property:

// Whit this line of code you will remove data-val-required DataAnnotationsModelValidatorProvider.AddImplicitRequiredAttributeForValueTypes = false;

// With these two lines you will remove data-val-number and data-val ModelValidatorProviders.Providers.Clear();
ModelValidatorProviders.Providers.Add(new DataAnnotationsModelValidatorProvider());


Thursday, September 11, 2014

ASP.NET MVC Razor Override "id" and “name” attribute with TextBoxFor

Use the code below as example to override the id or/and name attributes with TextBoFor, something really important to keep in mind is that "id" and "Name" is case sensitive.
    
   @Html.TextBoxFor(x => x.Data, new { Name = Model.Name + "_Custom", id = Model.ID + "_Custom" })


Thursday, August 7, 2014

How to link to a root controller from area controller in MVC

Sometimes you need to call a root controller from inside of an area, usually when you are inside of an Admin console for example.
The process is really simple, all you have to do is specify the area as blank in the RenderAction or the RedirectToAction functions.



        <% Html.RenderAction("Action", "Controller", new { area = "" }); %>


        public ActionResult redirect()
        {
            return RedirectToAction("Action", "Controller", new { area = "" });

            return RedirectToAction("Action", "Controller", new { area = "", id = 5 });

        }


Thursday, July 3, 2014

MVC razor decode / encode HTML

Razor has the ability to automatically encoded html.
In order to avoid encoding use the HTML helper: @Html.Raw(text).



@{ var br = "
";} This is the line number 1 @Html.Raw(br) This is the line number 2 @Html.Raw(br) This is the line number 3 @Html.Raw(br)

Monday, June 9, 2014

ModelState.AddModelError to add a simple validation

Use ModelState.AddModelError to add a simple validation and make the ModelState.isValid with errors.



   // Validation logic
   if (MyData.Email.Trim().Length == 0){
        ModelState.AddModelError("Email", "Invalid Email");
   
        return View(MyData);
   }


Tuesday, June 3, 2014

Using @Html.ActionLink as image link

If you need to serve image with the link using Razor, you can use HtmlHelpers like Html.ActionLink() to make it possible, this is how you can do it. First define the style with the image:

a.link_logo {
    background:url(/images/logo.jpg) no-repeat top left;
    width: 355px;
    height: 75px;
    display: block;
    text-indent: -9999px; /* In order to hide the text */
}



Then use the HtmlHelper and add the style:

// --------------------------------
@Html.ActionLink("Home", "Index", "Home", null, null, null, null, new { @class = "link_logo" })


// --------------------------------
@Html.ActionLink("Home", "Index", null, new { @class = "link_logo" })


//---------------------------------