Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 1090097
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T23:19:17+00:00 2026-05-16T23:19:17+00:00

I have a Create view which is passed a ViewModel. The ViewModel contains the

  • 0

I have a Create view which is passed a ViewModel. The ViewModel contains the following:

namespace MyProject.ViewModels
{
    public class MyObjectCreateView
    {
        public MyObject MyObject { get; set; }
        public List<OtherObject> OtherObjects { get; set; }
    }
}

The objects are generated using Entity Framework. I have a metadata partial class for MyObject:

[MetadataType(typeof(MyObjectMetaData))]
public partial class MyObject
{
    // Validation rules for the MyObject class

    public class MyObjectMetaData
    {
        // Validation rules for MyObjectId
        [DisplayName("MyObject")]
        [Required(ErrorMessage = "Please enter the MyObject ID number.")]
        [DisplayFormat( ApplyFormatInEditMode=true,
                                ConvertEmptyStringToNull=true,
                                HtmlEncode=true)]
        [DataType(DataType.Text)]
        [StringLength(25, ErrorMessage = "Must be under 25 characters.")]
        public object MyObjectId { get; set; }


        // Validation rules for Title
        [Required(ErrorMessage = "Please enter the Title for MyObject.")]
        [DataType(DataType.MultilineText)]
        [StringLength(200, ErrorMessage = "Must be under 200 characters.")]
        [DisplayFormat(ApplyFormatInEditMode = true,
                                ConvertEmptyStringToNull = true,
                                HtmlEncode = true)]
        public object Title { get; set; }

Etc…

My Create view looks like this:

<% Html.EnableClientValidation(); %>

<% using (Html.BeginForm()) {%>

    <%:Html.ValidationSummary(true, "Please fix the following errors:") %>
    <%:Html.EditorFor(model => model.MyObject) %>

    <p>
        <input type="submit" value="Save" />
    </p>

    <% } %>

<div>
    <%:Html.ActionLink("Back to List", "Index") %>
</div>

Finally, the editor for MyObject:

<%@ Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl<TxRP.Models.MyObject>" %>
<%--EditorTemplate--%>

<fieldset>
    <div class="editor-label"><%:Html.LabelFor(model => model.MyObjectId)%></div>
    <div class="editor-field">
        <%:Html.TextBoxFor(model => model.MyObjectId)%>
        <%= Html.ValidationMessageFor(model => model.MyObjectId) %>
    </div>           

    <div class="editor-label"><%:Html.LabelFor(model => model.Title)%></div>
    <div class="editor-field">
        <%:Html.TextAreaFor(model => model.Title, new { cols = "80" })%>
        <%= Html.ValidationMessageFor(model => model.Title)%>
    </div>

I have Client validation set, and all the scripts are in the master page:

<script src="../../Scripts/MicrosoftMvcAjax.js" type="text/javascript"></script>
<script src="../../Scripts/MicrosoftMvcValidation.js" type="text/javascript"></script>
<script src="../../Scripts/MicrosoftMvcJQueryValidation.js" type="text/javascript"></script>
<script src="../../scripts/jquery-1.4.1.min.js" type="text/javascript"></script>  
<script src="../../Scripts/ui/minified/jquery.ui.core.min.js" type="text/javascript"></script>  
<script src="../../Scripts/ui/minified/jquery.ui.datepicker.min.js" type="text/javascript"></script>  

<link href="../../Content/Site.css" type="text/css" rel="Stylesheet" />  
<link href="../../Content/jquery-ui/sunny/jquery-ui-1.8.4.custom.css" type="text/css" rel="Stylesheet" /> 

When I click the Save button, no validation happens. No client validation, no server validation (it doesn’t even seem to hit the Post create action at all!); it just bombs on the entity framework model with a ConstraintException, because Title is null. Argh!

I’m sure it’s just some silly thing I’ve overlooked, but I know I had it working at one point, and now it’s not, and I’ve been trying to figure this out all week. Thanks for any help, I’m developing a callous on my forehead from banging it on my desk!

EDIT: Here is the controller:

public ActionResult Create(MyObject myObject)
{
    if (!ModelState.IsValid) 
    {
        //ModelState is invalid
        return View(new MyObject());
    }
    try
    {
        //TODO: Save MyObject

        return RedirectToAction("Index");
    }
    catch
    {
        //Invalid - redisplay with errors

        return View(new MyObject());
    }           
}

and the exception stack trace:

at System.Data.Objects.DataClasses.StructuralObject.SetValidValue(String value, Boolean isNullable)
   at MyProject.Models.MyObject.set_Title(String value) in C:\CodeProjects\MyProject\Models\MyProjectDB.Designer.cs:line 4941
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-16T23:19:18+00:00Added an answer on May 16, 2026 at 11:19 pm

    Couple of remarks about your code:

    1. You say that you have a view model but what I see looks more like an Entity Framework autogenerated model. Properties of type object, … arghhh…
    2. You include both MicrosoftMvcValidation.js and MicrosoftMvcJQueryValidation.js but those are mutually exclusive. You have to decide whether you will do client validation using MS Technology or jquery validate plugin.

    You didn’t show any code of the controller actions nor the exact exception stack trace you are getting. You are saying that the Post action is not hit but you get an exception. Where do you get this exception?

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.