Tracking Forums, Newsgroups, Maling Lists
Home Submit Tracker Forums
  Advanced Search


C# - MVC View Model Text Data Lost After The Post - Display Them In The Case Of Failed Validation?

I've got a view model that has some text properties. And I use Html.DisplayFor to display them on the screen. But those text data won't be post back, so in the case of failed validation, the returned view won't have those data. How do I handle this kind of situation?

View 2 Replies (Posted: Nov 12 10 at 4:34)

Sponsored Links:

Related Forum Messages For ASP.NET category:
Data Of Text Box Is Lost After Partial Post Back?
I have used update panel and in that update panel I have kept ASP Table control in which I am creating rows dynamically which in turn contains 1 dropdown lists and 3 text boxes. My problem is that after partial postback text boxes and dropdown list are found but text property of text boxes is showing empty and dropdown list's selected value is set to the first record in that box. I have also stored entire table in session and retrieve it in(!isPostBack) condition at page_load event.

part of my .aspx page is as below


I want to add one row on every addRowTbn click event there by retaining previous rows.

Posted: Mar 18 at 10:59

View 1 Replies!   View Related
MVC :: How To Display Model Level Custom Data Annotation Validation Error
I am a MVC newbie & am lost in various ways validation can be implemented in my application.

I created a custom model-level data annotation validator attribute, but am unable to display its error message in the view. Basically, I have let's say 5 properties in the Entity class Job (model-level custom attribute called UniqueKeywords defined on it):

1) LoginID: value comes in the URL

2) Title: Required property level attribute defined on it

3) CatID1, CatID2, CatID3 - 3 categoryIDs - these are dropdowns in the view with same list of keywords in all 3.

I want to mandate that the values picked by the user in all 3 category dropdowns should be different.

With reference to the code pasted below, here is the explanation of what happens:

When I submit the form without specifying a title or picking anything from any of the 3 category dropdowns, the validation occurs for the property level Required attribute as well as model level uniquekeywords attribute, but the error is displayed only next to the required field "Title". I can confirm that the custom validation also works by filling in some text in the Title field & then re-posting the form...this re-displays the view, but the error message "Category cannot be duplicated" is not displayed.

Only relevant code sections are pasted below:

My Entity class code:


Posted: Nov 30, 2010 09:09 PM

View 10 Replies!   View Related
Web Forms :: How To Update View Source Of Page In Case Of Required Field Validation Failure
I have an ASP.Net screen, which has got around 10 required field and regular expression validators.

If an user clicks submit on the screen, the validators fire based on the user's input. It display the error messages on the screen.

Now I have to update the view source of the page to pass these error messages received by the user. If I am not wrong,the view source of the page gets generated at the server on Page_Load. And because the validators fire at the browser and they do not give any response back to the server I can't pass these error messages into my view source?

Posted: Sep 16, 2009 01:51 PM

View 3 Replies!   View Related
C# - Way To Display The Text In An Textbox As Upper Case
I need to DISPLAY the text in an asp textbox as upper case, regardless of whether the users caps lock is on or not - is there some property or method I can use?
I am converting the text using the .ToUpper method on submitting to the database anyway, but I have been asked to also DISPLAY it in upper case before that when first being typed.

Posted: Sep 27 10 at 9:27

View 1 Replies!   View Related
Read A Complex View Model On POST?
I have an user view model that has the following properties:

public User user;
public List<Language> Languages;

I send the above model to the view and use html helpers to build the form, so I end up with something like:

<form action="/Users/Edit/5" method="post"><input id="user_UserId" name="user.UserId" type="hidden" value="5" />
First Name
Last Name

Now, I try to read the POST in something that initially was something like :

[AcceptVerbs( HttpVerbs.Post )]

public ActionResult Edit( int UserId, FormCollection form ) {

and cannot get the user.UserId variable, user.FirstName variable etc.

what needs to be done to be able to read this kind of POST request. I'm kind of reluctant to modifying my ViewModel as it is very simple and easy to maintain as it is.

Posted: Jun 5 10 at 20:33

View 2 Replies!   View Related
Binding View Model From A Form Post With Inner Complex Types?

What I want to achieve is the ID of the property: item.Item.ID to be bound to the ID from the (route)URL. However I can't get it to work. I tried using [Bind()] attribute.

I fixed it now using a hidden field in the form like so:

<%= Html.HiddenFor(model => model.Item.ID)%>

However this feels a bit icky. I'd like to know if there is a better cleaner way of doing this?

Posted: Apr 15 10 at 7:34

View 1 Replies!   View Related
MVC :: Multiple Model Validation In Same View?
I have a Order view with2 user controls in it.

The first user control in the view is just to validate the customer and get some more details about the customer. This control is having one text box (customer name)with the button. When the user clicks the button it shouldvalidate the customer name in the client side using data annotations of customer model. if validation success then it should use some bussiness logic to verify the customer and show the customer details on the form.

The 2nd user control in the view is to get the address of the customer by searching for the house no and the postcode. This control is having 2 text box with the button.

The first user control is based on the strongly typed customer entity

The 2nd user control is bsed on the strongly typed address entity

The view with both the user controlis based on the strongly typed order entity.

The issue here is with the validation. I have the validation summaryin the view and client sider validation is enabled.When i click the search button on the first user control theclient validation starts and throws validation error messages because when i search for the customer at that point of time i willnot have values to may of the customer relatedfields in the order view.

The same thing happen when i click the search button on the 2nd view. It will throw client side validation for other fields.

What i want to achive is that when the user click the search button on the user control 1 then the validation should happen only to the customer name field (both in client and server).

When the user click the search button on the 2 nd user control the the validation should happen onlyfor houseno and the postcode both in client and the server.

When the user clicks the save button on the order view all the fields in the form even the controls usere input fieldsshouldget validated. How to achive this in mvc?

Posted: Jul 10, 2010 11:15 PM

View 4 Replies!   View Related
Repopulate The View Model In MVC 2 After A Validation Error?
I'm using ASP.NET MVC 2 and here's the issue. My View Model looks something like this. It includes some fields which are edited by the user and others which are used for display purposes. Here's a simple version

public class MyModel
public decimal Price { get; set; } // for view purpose only
[Required(ErrorMessage="Name Required")]


But doing this, I get an error on UpdateModel(rec): The model of type 'MyModel' could not be updated.

Posted: Mar 15 10 at 16:02

View 3 Replies!   View Related
Forms Data Controls :: Search According To Id Or Case No Then View It In A Grid View?
i'm tring to search my table according to id number or the case number then view it in a gridviewbut it work for the first time then after it stop for error

Dim conn As New SqlConnection
conn.ConnectionString = "Data Source=.SQLEXPRESS;AttachDbFilename=C:Program FilesMicrosoft SQL ServerMSSQL10.SQLEXPRESSMSSQLDATATask.mdf;Integrated Security=True;Connect Timeout=30;User Instance=True"


Posted: Aug 19, 2010 09:21 AM

View 3 Replies!   View Related
MVC :: Dropdownlist Validation Not Working For Complex View Model
I have a question regarding validation of drop-down list values. I have a view that is bound to a view model type calledReservationData.

This object contains a propertyCustomerVehiclesof typeList<VehicleData>.VehicleDatahas twointpropertiesVehicleMakeIdandVehicleModelId.

On my view I am trying to loop over the number of items in theCustomerVehiclescollection and displaying two dropdowns for each, a vehicle make dropdown and a vehicle model dropdown usingDropDownListFor.

When I try to submit and validate I do not see any validation errors displayed on the screen.

Just in case you are wondering I have added aValidationMessageForfor each dropdown as well. I am not sure if this is an issue with the structure of my view model and its complexity and how the controls need to be named or how the ids need to be set.

Here is the code for the looping over the collection:


Posted: Feb 03, 2011 02:19 AM

View 11 Replies!   View Related
MVC :: Multiple Instances Of Same Partial (with Same View Model) Messes Up Validation?
This project I'm working on have this custom clientside JavaScript validation framework created for (which I cannot change) that show error messages/summary based on your data annotations and a "Validate" method in your ViewModel.

Like I said I'm not allowed to change this, so wont be able to go with suggestions doing that.

What the person that created this did not foresee, is that one might need put several partials of the same type and having the same view model (each in a form) on a view.

So I went ahead and did things the normal way (using Html.whateverFor<model => model.whatever) and wala!....the custom validation thing throws error messages for the specific form fields, next to each form in the view.I was suggested to create a "prefix" for each instance of the view model, and do something like this:

Change: <%: Html.HiddenFor(model => model.AccountNumber)%>

To: <%: Html.Hidden(Model.ElementPrefix + "AccountNumber", Model.AccountNumber)%>

Not to mention the tons of jquery selectors and all that I have to go change (and all my view inputs)

(I just hate having to find workarounds for this "custom" everything they created for this project)

Posted: Jan 27, 2011 08:40 AM

View 9 Replies!   View Related
MVC :: Compound View Model Object Causing Remote Validation Failure?
I have used a pattern of compound view models for a few of the forms in this project. It works very well.

In this case I have a VendorAddress view model. I use Address(es) in a few places in this project so I made and Address view model object that I can re-use. Then I desided I want to do a very thorough checking of the state codes and zips so, I desided I would try to use remote validation and check the state and zip code aganst a stored set of tables in the database.

The problem is I am not getting values back to the remote action methods for the fields I'm asking for (in this case State and PostalCode). I believe this is because of the name mangleing that the MVC framework does for compound or sub types for id(s) for the inputs in this case it is making names like 'Address_Line1' and 'Address_State' and 'Address_PostalCode'.

Ultimately the question I have is, is there a way of turning off the pre-pended 'Address_' from the base view model object in a way that MVC framework can put it bak into the object after ther post of the form?


Then in the form after rendering it looks like this:


I get the remote call but there is nothing in the parameters "State" and "PostalCode", so the unobtrusive routines are firing but I think it does not know where to look for the data even though I have type something in the mensioned fields. As you can see in the rendered code the id="Address_State" and the data-val-remote-additionalfields="*.State,*.PostalCode"

Posted: Feb 12, 2011 08:26 PM

View 4 Replies!   View Related
MVC :: How To Display Property Of Model On View
How can I show display property of Model on View.

public class Model
[DisplayName("First Name")]

Posted: Aug 26, 2010 06:28 PM

View 6 Replies!   View Related
MVC :: How To Display An Image Property Of A Model In A View
this is my first post on this forum, and here it comes:I have a Model which has an image property:


Posted: Sep 11, 2009 07:29 AM

View 3 Replies!   View Related
MVC :: Display Foreign Key-bound Property From Model View
but I am new with MVC and database-driven applications.

I hava a table with Cities (ID,Name) and one with Businesses(ID,CityID,Name). The later has a foreign key relationship with the first table.

I used a linq-to-SQL class to create my models.

My problem is that I need to access the City Name(not the ID) when displaying the Business Model View.

How can I do this? if from my controller I call the View passing the auto-generated Business Model, I only have access to the ID.

Posted: Apr 17, 2010 08:38 PM

View 2 Replies!   View Related
MVC :: Display Changed Values In The View After POST?
I am trying do display the updated value in the View(in textbox and checkbox)after the submit button is clicked(POST). The values for textbox and checkbox areNOT getting updated only ViewData["Message"] is updated at every POST.

what am I doing wrong??? How do I display the updated values after everyPOST.

My View is as below:


Posted: Nov 27, 2009 05:01 AM

View 3 Replies!   View Related
MVC :: "Post" The Model Variable In View To The Next Controller?
I'm displaying the values in ViewData.Model in a View (.aspx) -- After the user looks them over, when the user clicks the "Submit" button that I have in this view, I want the ViewData.Model passed to another controller, but I'm not sure how to do this.

Currently, my target controller/action looks like this:


The "ViewData.Model" variable in this View is of type: List<Sample>

How do I pass the ViewData.Model to the RegisterController / Samples (action) ?

Posted: Jul 22, 2010 06:40 PM

View 12 Replies!   View Related
Forms Data Controls :: Display Text For Empty Grid View?
I'm trying to execute a query and display the result in grid view.Its working well...

However,I wish to display some simple text like"No tuples selected" making gridview visible to false if there are no rows selected for that query executed.Is there a way to do that?

Posted: May 05, 2010 02:53 PM

View 2 Replies!   View Related
MVC :: Bind Text Box To Model Property Not Working On "post Back"?
i have a text box that i bind to my model data and it works fine. however when i submit my form in some cases i want to changethe value that in the text box so some value i pull from the db. when i do this, it only works if the form was not submitted. is there something i am not aware of when it comes torebinding a text box from the modelin cases when the form was submited, instead of reloading the same value thatthe userentered into the text box?

Posted: Jan 22, 2010 09:50 PM

View 1 Replies!   View Related
Should Data Annotations Be On The Model Or The View Model
I've been used to decorating data model classes with data annotation attributes, but the purist in me baulks slightly at including purely presentational attributes such as display format here. I am, however, quite happy to keep validation centric attributes here. One good reason I have to continue keeping all annotations etc. in the data model is that my view model aggregates data model classes, e.g.

my ViewModelBase.DetailItem<TEntity> property in the view model is just a reference to an entity class in my data model. If I wanted to move presentational annotations to the view model, I would have to quite radically revise my design to one where I duplicate data model properties in my view model and use an object mapping tool to populate view model objects based on data model objects.


Posted: Dec 15 10 at 16:13

View 2 Replies!   View Related
Display Text Returned From Json Post Call?
I have the following javascript:

$.post("/Authenticated/DeletePage/" + PageId);
showStatus("Page deleted...", 10000);

I would like to instead pass showStatus() text that is returned by the $.post() call, rather than hardcoded text. How do I do this?

Posted: Mar 15 10 at 18:45

View 2 Replies!   View Related
Model View Control Versu Model View Presenter
I have been reading about different model for development

model view control mvc

model view presenter MVP

Model view view modelMVVM

i belive MVC has two big Advantage over webform 1) TDD 2) More control on HTML

MVP is bit variation in mvc model. rapid development as well as 1) TDD 2) More control on HTML(correct me if i m wrong) see the below link


Posted: Jun 02, 2010 04:45 PM

View 7 Replies!   View Related
Forms Data Controls :: How To Get Data To Details View In This Case
I try to bind a detailsview to my SQL Server data base, but I dont know how to get specific field like this case. this is my C# code


this is my HTML code, in line 10 in wizard I can get "title" by using "<%# Eval("Title") %>" ,is that right? I want to get the same title but not in wizard


Posted: Feb 14, 2011 05:19 AM

View 3 Replies!   View Related
MVC2 View Model For Multiple View Forms And Data
one thing that has been puzzling me since learning MVC2 is the following case scenario: I have a view which contains two latest news lists, a login form and a signup form. Every example I found on Views and View Models so far has a one-to-one example such as a simple login form etc. But how do I create a model that provides the properties and validation for a login and signup form and manages the data for the news lists. can I pass multiple models in the strongly typed view? When I created one model the form validation would fail as it expects all fields - login and signup to be filled. I am missing some advanced examples or information.

Posted: Aug 26 10 at 9:39

View 2 Replies!   View Related
MVC :: Access Model Validation Inside Custom Model Binder?
Is it possible, inside a Custom Model Binder, to fire "something" that "says" the value is invalid so it gets handled by validation part?

Basically, I am getting an exception when the value for the property is invalid.

Posted: Sep 01, 2010 07:23 PM

View 1 Replies!   View Related
MVC :: Data Lost In Transition From View - Controller
MVC basics or looks like I am doing somethingterriblywrong, but have not slightly idea what is it? Want to delete user from MVC Framework's Built in Security Module. So first I define Delete() in AccountController:


and this works fine. Second: I added view (for delete) DeleteUser.aspx

<h3>Are you sure you want to delete this?</h3>
<div class="display-label">UserId</div>
<div class="display-field"><%= Html.Encode(Model.UserId) %></div>
<div class="display-label">UserName</div>
<div class="display-field"><%= Html.Encode(Model.UserName) %></div>
<div class="display-label">LoweredUserName</div>
<div class="display-field"><%= Html.Encode(Model.LoweredUserName) %></div>
.... etc
And this works fine.
And then finaly when I submit this values for delete, and run back to the Controller and DeleteUser:
public ActionResult DeleteUser(aspnet_Users inUser)

It is a problem. Not a single value from View are pased?? For example:

inUser.UserName is null,
inUser.UserId 00000000-0000-0000-0000-000000000000,
... etc.

All values form view are losted? Why and how this can be?

Posted: Sep 28, 2010 11:02 AM

View 4 Replies!   View Related
How To Bind Post Data With A Model That Contains Multiple Models
I have a parent View that uses a few partial views. Each partial view has its own custom model. The parent View is a model containing each of the partial view's model. In other words this parent View model is NOT flat.

How do i get POST data to bind with the parent's model object? Do i have to write a custom model binder?

Update I'm not binding to a collection of objects of the same type (ie: a collection of uploaded files). I'm binding to object that contains multiple of objects. For example:


Posted: Nov 4 10 at 20:51

View 1 Replies!   View Related
MVC :: Retain Partial Data In Model Over The Post Backs?
I have recently started learning ASP.NET with MVC 2 Framework, I am facing difficulties in the following scenarios.In my MVC application I want to retain partial information in Model over post backs.Following is my scenario,

1. I initiate "Edit" of main record by clicking date hyperlink from left navigation.

2. Amend some data.

3. Save the record.

4. "Edit" sub record of the saved record by clicking another hyperlink from bottom left navigation panel.

The page is divided in to 6 tabs, the first 5 tabs shows the data for the main record and the 6th tab shows data of sub record. Wehn we "Edit@ sub record the previous 5 tabs doesn't have any changes noted, that means there is no harm in keeping their details intact in the main model. So is there a way to access model in the controller on hyper link click action so that I can only load the data for the clicked sub record (I am asking this, because the hyperlink click uses HTTP GET method so no model parameter passed in action method). If no, then how do I retain partial values in the Model over post backs?

Note that each tab data have been kept in individual models, and a main model has a collection member to hold all these individual models together.I thought as the model is binded with the view, it is too binded with controller. But it is allowing easily to access model in variable "Model" for the view but not for the controller.

Posted: Oct 12, 2010 04:40 AM

View 3 Replies!   View Related
Forms Data Controls :: Set Width Of Gridview In Case Of Garbage Text Which Has No Space?
if we enter text without space intext boxand then display it in gridview then itdoesn'tbreak in multiline anddisturball GUI. i try to set column width but itdoesn'tworking.

Posted: Feb 03, 2011 08:55 AM

View 6 Replies!   View Related
How To Display Tooltips Instead Of Plain Text When Form Validation Fails
I have been asked to display tooltips instead of plain text when form validation fails. We are currently using MVC 3 data annotation validators to display validation error messages. I am still fairly new to MVC and I have spent hours online looking for a clean solution

Posted: Feb 6 at 4:35

View 1 Replies!   View Related
Visual Studio :: Failed To Load Entity Data Model Tools Package - 2147467259
I am getting the following error when trying to add entity data model object for the Movie database

Failed to load Entity data model tools package - 2147467259

I am using Visual studio 2oo8 team foundation developer edition, vista business .

Posted: Jun 26, 2009 06:15 AM

View 1 Replies!   View Related
Web Form And Ajax Data Lost On Post Back - Finding Possible Cause / Ajax Or Changing Visible Property
I have two Repeater controls, each hosted in a user control. Both user controls are contained in the same aspx page. Only one User Control is visible at any one time. The repeaters are comprised of checkboxes, and text boxes for user input.

The aspx page is configured with an Ajax ScriptManager; and contains several Ajax UpdatePanels. These UpdatePanels result in partial page post backs when text is changed in a textbox control in controls on the aspx page (this is not the behaviour for the Repeaters in the User Controls). Through use of several AsyncPostBackTriggers, various controls contained in the other UpdatePanels on the page have their content refreshed in response to the partial page post backs they are configured be notified about.

Depending on a radio button group selection, I set the visible property to true or false - as appropriate for the User control containing a repeater control. The Repeater control is then populated with data using databinding. All of this works.

However, when the Submit button is clicked, the Repeater control contains no data.

Given that I am not dynamically adding the Controls containing the Repeater controls (but using Visible true / false). I would have thought that the State of the fields and the data in the visible control would be preserved during the post back.

The User Controls are contained within the UpdatePanel that contains the Submit Button.

I have explicitly Enabled View state without any effect.

Am I correct in assuming that I should not have to do any explicit handling of data changes the user makes (via client side script and manipulation of an Data Structure Representing the Repeater Data); and the View State should maintain the data I need to access on the server when submitted?

I do not believe that it is the User Control visible state changes that are causing the issue because when the page is initially loaded on of the User controls is populated with dummy rows (so it displays).

I am suspicious that because the visible state of the controls is changed during partial page post back, that the Page View State ends up with no knowledge of the User Control and therefore cannot track its data (or changes).

Posted: Sep 15 10 at 19:56

View 1 Replies!   View Related
Forms Data Controls :: Grid View Row Command Event Lost?
The grid view row commanddoes not execute on my production environment. This is a intermittant issue. I am not able to reproduct the exact steps but it does occur too often.

I havea loadbalanced environment with 2 identicalservers in the farm. The servers are running on IIS 7.5

The other scenario in which I observe this is when the session times out. In that case the grid view events are lost. But this scenario is not a priority for me. I need to resolve the first scenario.

Any help on this or some useful links on "Understanding the session" or "understanding the complete event firing and execution" is welcome.

An update: we are overriding the PageStatePersister as:


Posted: Jan 31, 2011 06:21 AM

View 4 Replies!   View Related
Visual Studio :: Design View Changes Lost In Source View?
I often losethe changes I make when editing an aspx page in design view, even if I frequently save and rebuild. When I close the project, then re-open it, the changes are gone. If I make the same changes in source view, it works fine.

Posted: Nov 06, 2009 07:45 PM

View 5 Replies!   View Related
MVC :: How To Use Attribute [Display(Name="")] For Model That Comes From An Entity Data Model
I am a newbie in mvc3 and i'm wondering how to use attribute like [Display(Name="")] for model that comes from an entity data model that I provide im my "Model" folder in my mvc3 project.

I didn't provide a .cs class for each of my database tables .

other words, I want the controller class render a edit form for me like :

First Name:--- instead of : fName:---

Posted: Mar 15, 2011 09:05 AM

View 9 Replies!   View Related
MVC :: How To Use Of Validation With Data Annotations In Model Classes
I make use ofvalidation with data annotations in my model classes. How do I make sure this annotations are not violated when I validate data in my business layer, before sending it to the DAL? I guess I don't want to define the validation rules in two places (model classes and in my BLL-classes)?

Posted: Dec 12, 2010 07:36 PM

View 6 Replies!   View Related
MVC :: Model Validation Of Excel Imported Data?
I useFileHelpers to import data from excel sheet to classes and then put it in MSSQL. Can somebody tell me if there is there a way to do model validation data imported from excel files?

Posted: Jan 23, 2011 05:23 PM

View 6 Replies!   View Related
MVC :: Scenarios Where Data Validation Is Done In The Viewmodel Not The Model?
point out an example or tutorial online that shows where data validation is done in the viewmodelas against the model? i saw a number of posts where that method is recommended..i dont really agree with it but i would like to see how it would work out..

Posted: Jun 08, 2010 04:34 PM

View 2 Replies!   View Related
Stop ObjectDataSource From Data Binding When Validation Failed?
I have an ObjectDataSource that is bound to a GridView. The object accepts a parameter from a TextBox. The problem I have is when I use a CustomerValidator with a ServerValidate event, the ObjectDataSource would still attempt to perform a DataBind despite the fact the the customer validator has returned false. The following is the code in the ASPX page.

<asp:TextBox ID="sittingDate" runat="server" />
<asp:CustomValidator ID="DateValidator" runat="server" ControlToValidate="sittingDate" OnServerValidate="DateValidator_ServerValidate" />
<asp:ObjectDataSource ID="BatchDataSource" runat="server" OldValuesParameterFormatString="original_{0}"
SelectMethod="GetOrCreateSittingBatch" TypeName="BatchBLL" OnSelected="BatchDataSource_Selected" OnSelecting="BatchDataSource_Selecting">
<asp:ControlParameter ControlID="sittingDate" Name="batchDate" PropertyName="Text"
Type="DateTime" />
<asp:GridView ID="BatchGridView" runat="server" DataSourceID="BatchDataSource">

In the Custom Validator I have

protected void DateValidator_ServerValidate(object source, ServerValidateEventArgs args)
/Ensure that the entered data is a date.
string input = args.Value;
DateTime result;
args.IsValid = DateTime.TryParse(input.TrimEnd(), out result);

How do I stop the ObjectDataSource from data binding when validation has failed?

Posted: Feb 17 10 at 1:54

View 2 Replies!   View Related
Copyright 2005-08, All rights reserved