ASP.NET MVC2 - usage of LINQ-generated class (validation problem)
Posted
by ile
on Stack Overflow
See other posts from Stack Overflow
or by ile
Published on 2010-05-03T12:13:05Z
Indexed on
2010/05/03
14:48 UTC
Read the original article
Hit count: 187
c#
|asp.net-mvc-2
There are few things not clear to me about ASP.NET MV2.
In database I have table Contacts
with several fields, and there is an additional field XmlFields
of which type is xml
. In that field are stored additional description fields.
There are 4 classes:
Contact
class which corresponds toContact
table and is defined by default when creating LINQ classesContactListView
class which inheritsContact
class and has some additional propertiesContactXmlView
class that contains fields fromXmlFields
fieldContactDetailsView
class which mergesContactListView
andContactXmlView
into one class and this one is used to display data in view pages
ContactListView
class has re-defined some properties from Contact
class (so that I can add [Required]
filter used for validation) - but I get warning message:
'ObjectTest.Models.Contacts.ContactListView.FirstName' hides inherited member 'SA.Model.Contact.FirstName'. Use the new keyword if hiding was intended.
ContactDetailsView
class is also used in a form when creating new contact and adding it to database.
I am not sure if this is correct way, and the warning message confuses me a bit. Any advise about this?
Thanks,
Ile
EDIT According to Jakob's instructions I tried it from scratch:
[MetadataType(typeof(Person_Validation))]
public partial class Person
{
}
public class Person_Validation
{
[Required]
string FirstName { get; set; }
[Required]
string LastName { get; set; }
[Required]
int Age { get; set; }
}
In Controller I have this:
[HttpPost]
public ActionResult Create(Person person, FormCollection collection)
{
if (ModelState.IsValid)
{
try
{
personRepository.Add(person);
personRepository.Save();
}
catch
{
return View(person);
}
}
return RedirectToAction("Index");
}
View:
<%@ Page Title="" Language="C#" MasterPageFile="~/Views/Shared/Site.Master" Inherits="System.Web.Mvc.ViewPage<Validate.Models.Person>" %>
<asp:Content ID="Content1" ContentPlaceHolderID="TitleContent" runat="server">
Create
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">
<h2>Create</h2>
<% using (Html.BeginForm()) {%>
<%= Html.ValidationSummary(true) %>
<fieldset>
<legend>Fields</legend>
<div class="editor-label">
<%= Html.LabelFor(model => model.FirstName) %>
</div>
<div class="editor-field">
<%= Html.TextBoxFor(model => model.FirstName) %>
<%= Html.ValidationMessageFor(model => model.FirstName) %>
</div>
<div class="editor-label">
<%= Html.LabelFor(model => model.LastName) %>
</div>
<div class="editor-field">
<%= Html.TextBoxFor(model => model.LastName) %>
<%= Html.ValidationMessageFor(model => model.LastName) %>
</div>
<div class="editor-label">
<%= Html.LabelFor(model => model.Age) %>
</div>
<div class="editor-field">
<%= Html.TextBoxFor(model => model.Age) %>
<%= Html.ValidationMessageFor(model => model.Age) %>
</div>
<p>
<input type="submit" value="Create" />
</p>
</fieldset>
<% } %>
<div>
<%= Html.ActionLink("Back to List", "Index") %>
</div>
</asp:Content>
When posting new person with no values, nothing happens (page is just reloaded). When posting with some values, person is added to db. I have no idea what am I doing wrong.
© Stack Overflow or respective owner