MVC :: JQuery Disabled TextBoxFor Causes Model Fields To Be Null?
Jul 19, 2010
So i'm working on the form that i started MVC with - the problem is that i'm NOT using a ViewModel for the view, its an actual Linq to SQL object. And so far it works fine. This form is a beast and id rather not refactor it to use viewmodels....
My big problem now is that depending some condition, i pretty much set almost everything to disabled when a particular dropdown is selcted (to mimic a deactivated state) - when the user hits submit, the model fields are now null, even tho you can see them filled in (just greyed out and unselectable) and validation refuses them.
Is there anyway around this other than to refactor the Views to use ViewModels (and set different validation rules so submit works)?
View 4 Replies
Similar Messages:
Mar 15, 2010
I'm using MVC 2 with some Models from a LinqToSql project that I built. I see that when I post back to a Controller Action after editing a form that has a DateTime field from the Model, the MVC Html.ValidationMessageFor() helper will nicely display an error beside the Date text box. This seems to happen automatically when the you test ModelState.IsValid() in the Controller Action, as if the MVC model binding automatically knows that the DateTime field cannot be empty.
My question is... I have some other string fields in these LinqToSql generated classes that are Not-Nullable (marked as Not Nullable in Sql Server which passes thourgh to the LinqToSql generated classes), so why doesn't Mr. MVC pick up on those as well and display a "Required" message in the ValidationMessageFor() placeholders I have added for those fields?
Sure, I have successfully added the MetadataType(typeof<t>) buddy classes to cover these Non-nullable string fields, but it sure does seem redundant to add all this metadata in buddy classes when the LinqToSql generated classes already contain enough info that MVC could sniff out. It MVC validation works with DateTime automatically, why not these Not-nullable fields too?
View 4 Replies
Apr 16, 2010
I have a textbox that I am defining as
<%= Html.TextBox("Username", Model.Form.Username,
new { @class = "textbox", @disabled = "disabled" })%>
In my action
[AcceptVerbs(HttpVerbs.Post)]
[ValidateAntiForgeryToken]
public ActionResult EditLogin(LoginForm post) {
return View(model);
}
post.Username will be blank, all other properties bind correctly, but if I change @disabled="disabled to @readonly="readonly" the username binds properly and everything works. It looks like model binding ignores values in disabled fields. Is their a way around this? I still need the field's value to bind to the model. I can use readonly but would prefer to use disabled so it is visually apparent to the user that they cannot edit the value of the field.
View 3 Replies
Dec 11, 2010
how to disabled and uncheck the checkbox values from database fields..if i have entry in my database records as :Name Seats DateSumit 1,3 11-Dec-2010Sanjeev 2,4 12-Dec-2010Manish 1,3,4 11-Dec-2010I want on page load event the default date for todays is 11-Dec-2010 then checkbox1, checkbox2, checkbox3, checkbox4 will be disabled and unchecked state for 11-Dec-2010as you see on the above database structure the seats for 11-Dec-2010 is 1,2,3,4 so the chekbox1, checkbox2, checkbox3, checkbox4 will be disabled..
View 1 Replies
Jan 6, 2011
How to avoid Model popup disabled on postback? I have a dropdown list in model popup control with autopost back="true". On post back am getting customer name. But on post model popup get disabled
View 2 Replies
Sep 15, 2010
I have a strange problem in my application.whenever i select a suggestion from the autocomplete(jquery) textbox, it first fires a blur event of a textbox first.(When i click the suggestion using mouse). But i want to fire blur event later.What should i do?
View 9 Replies
Apr 6, 2010
If a disabled drop-down list is dynamically rendered to the page, it is still possible to use Firebug, or another tool, to tamper with the submitted value, and to remove the "disabled" HTML attribute.
This code:
[code]....
The problem occurs when I use Firebug to remove the "disabled" attribute, and to change the selected option.On submission of the form, and re-creation of the field, the newly generated control has the correct value by the end of OnLoad, but by OnPreRender, it has assumed the identity of the submitted control and has been given the submitted form value.
.NET seems to have no way of detecting the fact that the field was originally created in a disabled state and that the submitted value was faked. This is understandable, as there could be legitimate, client-side functionality that would allow the disabled attribute to be removed.
Is there some way, other than a brute force approach, of detecting that this field's value should not have been changed?
I see the brute force approach as being something crap, like saving the correct value somewhere while still in OnLoad, and restoring the value in the OnPreRender. As some fields have dependencies on others, that would be unacceptable to me.
View 4 Replies
Oct 27, 2010
I have a problem with passing a model to a controllerIn my controller I have a HttpPost function that gets a EF model.The problem is that the form does not contains all properties of the EF model.When the function is called that gets this EF model, I get error messge of fields that are not nullable in the EF model.That's because the fields are not in the form, so the not nullable properties gets no value.
View 5 Replies
Dec 26, 2010
One of the fields on the view does not have a label when i ran the project, although the other fields got labels and the following HTML code to generate the label already exists:-
<%: Html.LabelFor(model => model.Article_Type_ID) %>
View 2 Replies
Jan 17, 2010
By picking MVC for developing our new site, I find myself in the midst of "best practices" being developed around me in apparent real time. Two weeks ago, NerdDinner was my guide but with the development of MVC 2, even it seems outdated. It's an thrilling experience and I feel privileged to be in close contact with intelligent programmers daily. Right now I've stumbled upon an issue I can't seem to get a straight answer on - from all the blogs anyway - and I'd like to get some insight from the community. It's about Editing (read: Edit action). The bulk of material out there, tutorials and blogs, deal with creating and view the model.
So while this question may not spell out a question, I hope to get some discussion going, contributing to my decision about the path of development I'm to take. My model represents a user with several fields like name, address and email. All the names, in fact, on field each for first name, last name and middle name. The Details view displays all these fields but you can change only one set of fields at a time, for instance, your names. The user expands a form while the other fields are still visible above and below. So the form that is posted back contains a subset of the fields representing the model. While this is appealing to us and our layout concerns, for various reasons, it is to be shunned by serious MVC-developers.
I've been reading about some patterns and best practices and it seems that this is not in key with the paradigm of viewmodel == view. Or have I got it wrong? Anyway, NerdDinner dictates using FormCollection och UpdateModel. All the null fields are happily ignored. Since then, the MVC-community has abandoned this approach to such a degree that a bug in MVC 2 was not discovered. UpdateModel does not work without a complete model in your formcollection. The view model pattern receiving most praise seems to be Dedicated view model that contains a custom view model entity and is the only one that my design issue could be made compatible with.
It entails a tedious amount of mapping, albeit lightened by the use of AutoMapper and the ideas of Jimmy Bogard, that may or may not be worthwhile. He also proposes a 1:1 relationship between view and view model. In keeping with these design paradigms, I am to create a view and associated view for each of my expanding sets of fields. The view models would each be nearly identical, differing only in the fields which are read-only, the views also containing much repeated markup. This seems absurd to me. In future I may want to be able to display two, more or all sets of fields open simultaneously.
View 5 Replies
Mar 20, 2011
How can i make this work like i have 5 fields in my database table . 2) i created a model for validation with datannotation and created the compare password field as well with comparepassword attribute. (now my model has 6 fields including one external field compare password and 5 database fields)3) i created a view with that model.(User)4) now i created a controller as well but the problem arise here that when calling that actionmethod the compiler complains about the external field and tells it cannot find any extension method . how can i create the extension method or solve this error?
[Code]....
View 5 Replies
Dec 9, 2010
I am beginner to MVC. I have created a View with dropdownlist, text box and text area. I didn't create a strongly typed view because these fields are not generating from the database. Is this right way of doing it? Do I have to create a 'Model' class and generate a strongly typed view? I am trying to validate(client and server side) these fieds before saving into the database. How to create a Model class for these fields?
Here is my view looks like.
[code]....
View 1 Replies
Feb 15, 2010
i just noticed that if my controller has a parameter with a name that matches the name of a field in my view model, and the view emits the value of the model's field, the value emitted is that passed in the query string, not the one set by the controller. is this by design?
here is what i mean:
my view model:
public class MyViewModel
{
int id;
string name; [code]....
sending a request to "create?id=1234" emits "1234" in the hidden field instead of "1".
View 5 Replies
Sep 28, 2010
I am facing few issues. I am using MVC2.I am developing a form which is being created dynamically based on its layout and all html attributes info stored in DB table. This dynamically generated form stores data(this table have all html field names as columns) in another db table(db is mysql).
I have created ViewModel class in which i get both design table entity objects and data table entity object. I, then, created strongly typed view based on that ViewModel. But the problem is when i rendering html fields using html helpers i.e
TextBox(Model.tblDesign.htmlField_name).it doesnt bind with the columns of Model.tblData whose names are same as of Model.tblDesign.htmlField_name. how can i bind fields of data table?
View 1 Replies
Nov 29, 2010
I would like to separate my attribute decoration(3-4 per field) by having them someplace else so that my code looks readable.Also the arguments passed to some attributes should come from a resource file.EG:[Required("Cannot proceed without entering *field_Name*")]
I need just [Required] Possible duplicate of this question(on which i couldn't resist offering a bounty) : Default resource for data annotations.
View 2 Replies
Feb 24, 2011
Model is coming up as Invalid Scenario is I have a custom ViewModel and certain fields don't show up on the screen (like Id & some computed fields etc..) so when the data gets posted back the Model.IsValid() comes to false Q Do I have to pre init these fields when I do create
ActionResult CreateOrder(int customerID)
{
var order=new OrderViewModel();
//shall I init the fields which are not being keyed in by the user?
}
View 4 Replies
Nov 8, 2010
So I have code as below
public ActionResult Create(AccountClassificationCreateViewModel ACCVM)
{
if (ACCVM.newMtn0130 == null)//Why ACCVM.newMtn0130 is null after [code]...
I wonder why the ACCVM.newMtn0130 become null after RedirectToAction? I already assign with "_ACCVM.newMtn0130 = newMtn0130".
ACCVM._Error didn't become null after RedirectToAction.
View 5 Replies
Aug 3, 2010
I have a group of radiobuttons. The field they are 'bound' to is of datatype tinyint (sql server), byte .NET. It is set to NOT accept nulls in the database column (col1) and no default value has been set for the column, or indeed, anywhere else.
View code as follows:
<ul> <li><%= Html.RadioButton("col1", "1")%></li> <li><%= Html.RadioButton("col1", "2")%></li> <li><%= Html.RadioButton("col1", "3")%></li></ul><%= Html.ValidationMessage("col1", "*") %>
If the user doesn't select any of them, I would expect null to be that the model would attempt to insert into the database. I would therefore get a validation error that I can display to the user, prompting them to choose one of the options.
However, instead of null, the model inserts a 0 into the database. As this does not fail, no error is raised.
I am not setting the default value to 0 anywhere, not in the mvc app or the database
View 8 Replies
Oct 30, 2010
Basically I have RegisterModel2 that has a Person class. My requirement is that Phone Number and Address are not required for registration. But if they try to enter a Phone number of Address then it should validate it. Problem is that it is always stating that the child fields of Phone (area code, number) and child fields of Address (street1,city,etc) are required. Is there a way to annotate in the RegisterModel that a parent class is not Required but if any data in a child element is given then and only
then should the child elements be validated?
My Person EDM class has a nullable 0.1 -> 0.1 navigation property to Phone and Address.Here is the person class
[MetadataType(typeof(Person_Validation))]
public partial class Person
{
}
public class Person_Validation [code]....
The Person EntityObject has an optional/nullable Navigation Property to the Phone Object.
The Phone object has these annotations,[Code]....
I have RegisterModel defined as follows:
[Code]....
My View looks like this:
[Code]....
And here is my Controller Action it is posting to:
[Code]....
View 1 Replies
Aug 17, 2010
I have a linq to sql entity class in which I declared some DateTime fileds like this:
[Code]....
And this is how I fetch data form repository:
[Code]....
All non DateTime fields are filled properly but all datetimes are null.[Column(DbType = "smalldatetime")]
View 8 Replies
Mar 6, 2011
When i run code error throw ,my model is null. When i try to debug controller method my debuger dont goto controller code, Why that?
View 3 Replies
Jan 19, 2010
My controller calls a class in my model which is getting data from a REST web service. The class in the model then passes the data back to the controller using an IList, like so:
[Code]....
My problem is this: The data that is held in the IList isn't there until the user has done a search in the View, submiting a form. This is a basic search function with 2 textboxes and a submit button in a form. When the user clicks submit, the controller action is called, the web service is then called, and the data in the IList gets populated. The problem is that in the View I am using a strongly typed view and my model reference in the foreach loop throws a Null Reference Exception because there is no data in it, because a search has not been conducted yet...
[Code]....
So, how can I have the above foreach loop in my view if there isn't any data in the model until a search has been conducted? Is there a better way that I should be doing this?
Essentially, what's the best method for passing data back to a view that is based on a form submittion?
View 6 Replies
Mar 12, 2010
I have several models that implement a array property using null object pattern in the following manner:
[Code]....
The problem is that any time I attempt to bind to a property of this type the DefaultModelBinder.CollectionHelpers.ReplaceCollectionImpl method wants to call ICollection<T>.Clear() and then ICollection<T>.Add() on the zero length array.
The real issue seems to be in DefaultModelBinder.BindComplexModel() method however. The method contains an explicit check for arrays and special handling to account for the fact that they don't support clear/add functionality. The problem is that the check stipulates that the array model must also be null. If it is non-null, normal ICollection<T> handling ensues which presumes support for clear and add. Can any one explain why the null check is there? It seems to me that either 1) the null check should be removed to permit replacement based binding to non-null arrays or 2) an explicit error stating that non-null arrays are not supported should be thrown.
I vote for the former. I can understand that in general, replacing an existing model with a refrence to a different model would be considered a bad thing. The calling code would not expect this in general and it could lead to hard to find bugs. However, in the case of arrays, if the calling code expects the array property to be modified at all, then I think the normal expectation would be that such modification would occur via replacement of the array.
View 1 Replies
Mar 31, 2011
Why is a null parameter being passed to the following controller action?
public FileContentResult GetImageForArticle(ArticleSummary article)
{
if (article == null || !article.ContainsValidThumbNail()) return null;
return File(article.ThumbNail, article.ThumbNaiType);
}
from the following partial view:
<%@ Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl<IEnumerable<AkwiMemorial.Models.ArticleSummary>>" %>
<%if (Model.Count() > 0) [code]...
View 1 Replies
Apr 4, 2011
There are multiple roles. (Role A, Role B etc) There are multiple input/output fields. (Field A, Field B etc) There are multiple permission levels controlling access to each field. (Read, Direct Edit, Edit With Approval, None) Each role has its own permissions to fields. (Role A has Read Permission to Field A; Role B has Direct Edit permission to Field A etc) Every role can be assigned to users and they are assigned by Geographic information. (User A is assigned to Role A for Continent: Europe - Country: Germany; User B is assigned to Role A for Continent: Europe - Country: France; User A is assigned to Role B for Continent: Europe - Country: France etc) Users can have multiple roles User identity is coming from Windows Authentication.
is it possible to represent this type of kind of multi-layered security model using ASP.NET internal membership/role providers? If so, what should my starting point be? Creating only custom role provider with custom methods and fields be enough?
View 3 Replies