.NET Tutorials, Forums, Interview Questions And Answers
Welcome :Guest
Sign In
Win Surprise Gifts!!!

Top 5 Contributors of the Month
Gaurav Pal
Post New Web Links

How to exclude one of model's property from the Html.EditorFor

Posted By:      Posted Date: September 10, 2010    Points: 0   Category :ASP.Net
Hi, I would like to exclude one of model's property from the Html.EditorFor. I tried  [HiddenInput(DisplayValue=false)]  but it renders the property as an hidden input. This could potentially be a security problem. How can we completely exclude a field from Html.EditorFor? Thank you,Max

View Complete Post

More Related Resource Links

Using Dynamic Property inside Html Helper with Razor fires an error



I have the following with Razor:

@Html.GoogleAjax(View.GoogleAjaxKey, new String[,] { { "jquery", "1.4.2" } })

And the I get a strange error:

  'System.Web.Mvc.HtmlHelper<object>' has no applicable method named 'GoogleAjax' but appears to have an extension method by that name. Extension methods cannot be dynamically dispatched. Consider casting the dynamic arguments or calling the extension method without the extension method syntax.

What is strange is that it works fine using:

@Html.GoogleAjax("123", new String[,] { { "jquery", "1.4.2" } })

I tried the following code line:


And the key is displayed just fine so the problem is how I use the value of @View.GoogleAjaxKey into Html.GoogleAjax.

Do I need to change something on my Html Helper? Here it is:

    public static MvcHtmlString GoogleAjax(this HtmlHelper helper, String key, String[,] libraries) {


Model Binding the values in an HTML Table back to Controller / Model


Currently, I am working with ASP.NET MVC1 and am still learning about Model Binding and how values from a View are passed back to the Controller / Model. Specifically, I want take an existing Model, create a Table and populate the Rows of the Table, allow the user to edit some fields and pass it back.

In my example, I have a Class called "Ingredient" which has 4 public accessors:  Name, Barcode, Amount, and Unit

<%@ Page Title="" Language="C#" MasterPageFile="~/Views/Shared/Site.Master" Inherits="System.Web.Mvc.ViewPage<IEnumerable<MvcACE.DomainModel.Entities.Ingredient>>" %>

<% using (Html.BeginForm("SubmitOrder","MyController"))
{ %>

<% if (Model != null)
foreach (var item in Model)
{ %>
<td><%= Html.Encode(item.Name)%></td>
<td><%= Html.Encode(item.Barcode)%></

HTML.RenderPartial vs HTML.EditorFor


This maybe a lame question but I wanted to understand the detailed differences between HTML.RenderPartial and HTML.EditorFor. I am using ASP.NET MVC2 with Framework 4.

Can someone point me to a link?

Model Display Property On View


How can I show display property of Model on View.


public class Model
        [DisplayName("First Name")]
        public string FirstName { get; set; }
        <div>FirstName</div><---Here I want to show Dispaly name
        <div><%= Html.Encode(Model.FirstName) %></div>

Html.RenderPartial appears to be stripping Model of custom attributes



I have implemented a custom ModelMetadataProvider so that I can decorate my view models with some custom attributes and everything was working fine until I made use of a Partial View.

The following code in my view works fine: -

<%: Html.DisplayFor(x => x.Results) %>

Results is a List which renders a custom display template and is also decorated with a custom attribute.  Using breakpoints, after the above line and prior to the code within the custom display template, the overridden CreateMetadata method in my custom ModelMetadataProvider is invoked.  If I look at the attributes collection parameter I can see that it does contain my custom attribute thus everything working as expected.

However, if I replace the above with the following line of code in my view then it breaks: -

<% Html.RenderPartial("ApplicationSearchResults", Model.Results, new ViewDataDictionary()); %>

All the Partial View contains is: -

<%: Html.DisplayFor(x => x) %>

Again using breakpoints, after the above line and prior to the code within the custom display template, the overridden CreateMetadata method in my custom ModelMetadataProvider is invoked.  But this time if I look at the attributes collection parameter my custom attribute is not there.

Any idea

Html.BeginForm and Returning Model

Any idea how can View return model(myModelValues) on the click of submit button,aspx code     <% Html.BeginForm("submit", "RenewalManager", FormMethod.Post); %>    <input type="submit" name="submitButton" onclick=" return confirmReject()" value="Test" />    <% Html.EndForm ( ); %> controller code        public ActionResult submit(string submitButton)        {            switch (submitButton)            {  //Cases...                case "Test":                    // call another action to perform the cancellation                     return (Confirm());  //...             }        }        public ActionResult Test()        {  

Validating Custom Types and Html.EditorFor() Templates?

I'm finding my self with the problem that if I do not show the Template for a "complex" object I have in the main view object I still get the errors in the summary. How do i get around this? I've got a model with say:   HouseType ->Persons (has its own editor template) ->Pets(has its own editor template)   In the create view I show each one depending if they are not null. But when I try to validate HouseType and say I have Persons not null and pets is null. I get errors of validation for pets. How do I get around this? -Thanks

Data Annotation not triggering the client side validation when used on an overriden model property

Hi all, This is a saga from a post that was recently answered:http://forums.asp.net/t/1598187.aspxMy main problem is the lack of client side validation when we use a child model class, the server side validation works like a charm whether you use the virtual/override solution or the new attribute on the child class with the getter and setter linking the property to the base class's one.The client side validation does not work with none of the solutions presented below.This is a sample code of the first solution: [Serializable] public class DateViewModel { ... [Display(typeof(App_GlobalResources.Messages), "Date_Year")] public virtual int? Year { get; set; } ... } [Serializable] public class BirthDateViewModel : DateViewModel { ... [Display(typeof(App_GlobalResources.Messages), "Date_Year")] [YearRange(ErrorMessageResourceType = typeof(App_GlobalResources.Messages), ErrorMessageResourceName = "Validation_Range")] public override int? Year { get; set; } ... } This is a code sample of the second solution : [Serializable] public class DateViewModel { ... [Display(typeof(App_GlobalResources.Messages), "Date_Year")] public int? Year { get; set; } ... } [Serializable] public class BirthDateViewModel : DateViewMod

Accessing Custom Property in HTML???


i have a usercontrol with a few dropdownlist....n i am using that usercontrol in few pages and depending on pages those dropdownlist  can be enabled mode or disabled mode.

so i created a property:

private string _readOnly;
        public string ReadOnly
         set {
                if (value == "true")
                    ddlOTime.Enabled = false;

Now i am accessing in HTML as

<uc3:OpenTime ID="OpenTime1" OTime='<%#Bind("OpeningTime")%>' ReadOnly="true" runat="server" />

it seems to work.

Just curious know that am i going in right direction or any better approach is available?


html meta property



Just wondered if anyone knows the answer to this as I can't find an obvious way to do it.

I programatically add meta tags to the head of each page using htmlhead, specifying the name and content.  I now need to add new meta tags with property and content - does anyone know how?

Changes to Properties in the Property Window not Updating in HTML Control


I'm using VWD 2010 Express and have found that when I change a property like AutoPostBack from within the property window the corresponding tag (<asp:TextBox...>) was not updated with the property change to the tag.

Is there a setting that I can change in the VWD properties that will enable changes through the property window for a control to automatically update the HTML tag for that control?

Returning a model and a html fragment


I have a web page with a form in the footer, when the user clicks submit in this form I submit their data and then return the view with the model with the line

Return View("TalkToUs", New TalkToUsModel(customer))

but this means that the user is at the top of the page and cannot immediately see the validation errors or success message on that page as this information is posted in the footer section so is there a way to return a view with the model and a html fragment so the url is 

http://website/page#footer so that they can see this straight away without having to scroll down.

Also I have to assume the user has javascript turned off and the user could be using browsers ie6 and greater.

Thanks in advance for any help.

Error when deploying a BCS model - SharePoint wants the SiteUrl property set, but it should not be n


Hi all,

We are getting the following error message when trying to deploy a BCS model as a feature:

The default web application could not be determined. Set the SiteUrl property in feature xxx_yyy to the URL of the desired site and retry activation.

The problem is apparently the missing SiteUrl property on the feature, which is needed to locate the BCS service - but we have deployed many times before without this property! The problem with this property is that it's environment specific, so setting it would make our WSP only good for one specific environment.

According to the following thread, we SHOULD be able to deploy BCS models without the SiteUrl feature (at least in RTM):


Any idea on how this can be solved?  Adding the SiteUrl is not an option since we can't possibly create separate WSPs for different environments.


Set radio-button selection from model's property


I got 3 radion-buttons on the view called:

- Taxi

- Train

- Bus

The model passed has a property called "Transport" of type String.

In the action, if I set the property as 'Train', then the view should set the radio-button 'Train' checked.



model.Transport = "Train";

How do I do that in the view to make the radio-button Train checked?

Set radio button as checked depends on the model property value


I got 3 radio-buttons:

<input type="radio" name"transport" value="taxi" checked /> Taxi <br />

<input type="radio" name"transport" value="train" /> Train <br />

<input type="radio" name"transport" value="bus" /> Bus <br />

I tried this but got error:

<input type="radio" name"transport" value="bus" <%: if (Model.Transport == "bus" ) { %> checked <:% } %>/> Bus <br />

The error was

CS1525: Invalid expression term 'if'

Any idea?

stupid -- why does model.Model break Html helpers?



This code in an aspx file:

      <%: Html.TextBoxFor(model => model.Model) %>

Breaks the page with the following error - which I think is stupid as Model is a property of my Product Entity and relates to the Model of the product (e.g. Make, Model, Year). If I remove this model.Model reference the rest of the Product properties render as expected. I am stuck with renaming my property?

Value cannot be null or empty.
Parameter name: name

Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code. 

Exception Details: System.Argu
ASP.NetWindows Application  .NET Framework  C#  VB.Net  ADO.Net  
Sql Server  SharePoint  Silverlight  Others  All   

Hall of Fame    Twitter   Terms of Service    Privacy Policy    Contact Us    Archives   Tell A Friend