MVC :: Model Not Validating Correctly In Controller. Optional Property With Required Children Fields
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]....
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?
Curious what the best practice is for returning errors to a view from a controller where the error isn't really a validation error, but more like "user not found" or "service timeout" type of errors. (in ASP.NET MVC2 framework)
I've been adding them to the ModelState's model errors, but that doesn't seem appropriate. (although easy to implement and maintain)
Example - A user tries to log in, and their credentials do not match a known user.
I've got a View where I allow input, the fields are set as float in my SQL Server 2008 R2 database and I am using Entity Framwork 4. In the Entity Framework Model the field looks like this private Nullable<global::System.Double> _TestNumber;
And the View uses an EditorField to allow input like this:
I am however getting this error in the Validation Message: The value '13.51' is not valid for TestNumber. I've tried with a comma instead of period, same thing.
I have the following scenario: my website displays articles (inputted by an admin. like a blog).So to view an article, the user is referred to Home/Articles/{article ID}.However, the user selects which article to view from within the Articles.aspx view itself, using a jsTree list.So I what I need to do is to be able to differentiate between two cases: the user is accessing a specific article, or he is simply trying to access the "main" articles page. I tried setting the "Articles" controller parameter as optional (int? id), but then I am having problems "using" the id value inside the controller.
I have a controller method that is called with many different combinations of URL's. To overcome issue i am using optional parameters.here is my controller method signature...public ActionResult localMembersSearch(string Gender, string calling, [Optional]string Region, [Optional]string County, [Optional]string Town, [Optional]string queryvalues)the first two parameters are not optional but always expect
Here's mt entity: Csharp Code: using System;using System.Collections.Generic;using System.Linq;namespace Venue.Domain{ using Venue.Infrastructure.Domain; public class Forum : Entity { private IList<Forum> children = new List<Forum>(); private IList<Moderator> moderators = new
[code]....
Here's my class map:
Csharp Code: using FluentNHibernate.Mapping;namespace Venue.Persistence.Mapping{ using Venue.Domain; public class ForumMap : ClassMap<Forum> { public ForumMap() { Id(x => x.Id); Map(x => x.Sequence); Map(x => x.Title);
[code]....
And here's my convention:
Csharp Code: using FluentNHibernate.Conventions;using FluentNHibernate.Conventions.Instances;namespace Venue.Persistence.Mapping.Conventions{ public class ReferenceConvention : IReferenceConvention { public void Apply(IManyToOneInstance instance) { instance.Column(instance.Property.Name + "Id"); } }}
I get the following message:Could not find a setter for property Children ...
My team is building ViewModels with model validation inside the MetadataType. My question is that I'm using a non-MVC project, can I use it to validate the model?
[MetadataType(typeof(PersonMetadata))] public class Person { public int ID { get; set; } public string Name { get; set; }
I added requiredvalidator to validate the input field in hte detailviews control. the only probably problem is error message shows up when form popup. I only want to show the error message after I click on the submit button. here's my code.
I am trying to find a way to have my Entity Framework model in a seperate project within my ASP.NET solution.
Currently I have my DataManager project (which contains my EF model and some classes) and a second project which contains all my web project files.
The problem that I have come across is that I have a database connection string in a App.Config file in my DataManager project and the same connection string in my Web.Config from my web project. I basically have a duplicate connection string.
Is there a way to only use only one connection string in my project (preferably from my web.config)?
My only concern is that when it comes to compiling my project I will not be able to change the connection string in the App.Config contained in my DataManager project.
My application has been developed in .net 2.0 platformwith a masterpage which doesn't have 'doctype' and was working fine. Now i had to use modelpopup extender from the Ajax Control Tool Kit 1.0 version. when I use this the popup , it is coming up but the page in the IE shows that this page seems to be increased by indefinite size. I found the reason as it was due to 'Doctype' which is not there in the master page. Later i have added the doctype to my master page, now the popup and everything works fine but the page design has got changed.
When on the view the validation doesnt fire stating that it is required - where as the other values do (such as strings or integer fields )- how does this work for Booleans?
I'm trying to write a form for a user to leave a testimonial on the website. I would like a group of inputs related to the users name (title, firstname and surname) or an input for their company name to be required. They can leave both if they like, but they must at least either fill in all the inputs related to their name or the input for their company name.can I do this with the RequiredFieldValidator?
i need to implement edit functionality for my model. among other fields my model contains record id in the database. when my controller is called i need to update the database record with the same id as the model using the fields in the model.
here is my controller:
[HttpPost] public ActionResult Edit(MyModel m) { var r = my_datacontext.MyTable.SelectSingleOrDefault(x => x.id == m.id); if (r != null) { r = m; my_datacontext.SubmitChanges(); } }
this does not work because m does not get added to the datacontext's ChangeSet. why? i fixed this by changing the signature of my controller action to receive FormCollection instead of the model, and by invoking UpdateModel manually. i think this is not the right way to do it.
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.
In MVCs with which I have worked, the Controller has the job of co-ordinating a number of views, consequent to some user action against the model.
However in ASP MVC, there never appears to be more than 1 view resulting from an http request (please correct me if I am wrong). Instead the "Controller" in ASP MVC appears to be a URL Routing Target.
Also, in my (admittedly limited) experience, the Model in MVC is intended to be a model of the problem domain of the application. However in ASP MVC, the "Model" appears to be a model of the data binding of the corresponding view.
Has anybody figured out a way to separate the dependency of the view/controller from the model. In other words, model is independent of the view/controller in asp.net MVC but has anybody also taken out dependency of the view/controller from model as well?
I've been going at this for several hours and I simply cannot find the solution.
I want to get some data from my user. So first, I use a controller to create a view which receives a Model:
[code]...
The rest of this controller does not matter since no matter what I do, the count attribute of Request.Files (or Request.Files.Keys) remains 0. I simply can't find a way to pass the files from the form (the Model passes just fine).