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 9321361
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 19, 20262026-06-19T03:50:23+00:00 2026-06-19T03:50:23+00:00

NOTE: Code updated to take into account the comments and answer made below. In

  • 0

NOTE: Code updated to take into account the comments and answer made below.

In my MVC app I need sometimes to make references to other objects (like a many-to-many relationship, or one-to-many relationship).

So I have this model:

public class ObjInfo
    {
        [Required(ErrorMessage = "Obj ID is required.")]
        [Display(Name = "ObjID")]
        public int m_Id { get; set; }

        [Required(ErrorMessage = "Obj Name is required.")]
        [Display(Name = "Obj Name")]
        public string m_Name { get; set; }

        [Display(Name = "Obj Number")]
        public int m_Number { get; set; }

        (...)

        [Required(ErrorMessage = "Other Obj is required.")]
        [Display(Name = "OtherObj")]
        public int m_OtherObjID { get; set; }

        public OtherObjInfo m_OtherObj { get; set; }

        (...)
    }

I have default and parameters constructors as well and can show them as needed, though I am not sure if they are at fault. Anyway.

In my controller, I have the two create methods following MVC methods:

        //
        // GET: /Obj/Create

        public ActionResult Create()
        {
            ViewBag.List = new SelectList(PopulateDDLs(), "OtherObj", "Other obj ID");
            return View();
        }

        //
        // POST: /Obj/Create

        [HttpPost]
        public ActionResult Create(Objinfo obj)
        {
            if (ModelState.IsValid)
            {
                m_ObjManager.CreateObj(obj);
                return RedirectToAction("SearchIndex");
            }

            ViewBag.List = new SelectList(PopulateDDLs(), "OtherObj", "Other obj ID");
            return View(obj);
        }

And, finally, here’s how my “Create” view is coded:

@model MyApp.Models.ObjInfo

@{
    ViewBag.Title = "Create";
}

<h2>Create</h2>

@using (Html.BeginForm()) {
    @Html.ValidationSummary(true)

    <fieldset>
        <legend>OBJ</legend>

        <div class="editor-label">
            @Html.LabelFor(model => model.m_Id)
        </div>
        <div class="editor-field">
            @Html.EditorFor(model => model.m_Id)
            @Html.ValidationMessageFor(model => model.m_Id)
        </div>
        <div class="editor-label">
            @Html.LabelFor(model => model.m_Name)
        </div>
        <div class="editor-field">
            @Html.EditorFor(model => model.m_Name)
            @Html.ValidationMessageFor(model => model.m_Name)
        </div>

        <div class="editor-label">
            @Html.LabelFor(model => model.m_Number)
        </div>
        <div class="editor-field">
            @Html.EditorFor(model => model.m_Number)
            @Html.ValidationMessageFor(model => model.m_Number)
        </div>

        (...)

        <div class="editor-label">
            @Html.LabelFor(model => model.m_OtherObj , "Other Obj")
        </div>
        <div class="editor-field">
            @Html.DropDownList(model => model.m_OtherObjID, ViewBag.List as SelectList, "--- Select Other Obj ---", new {@class = "OtherObjInfo "})
            @Html.ValidationMessageFor(model => model.m_OtherObj)
        </div>

        <p>
            <input type="submit" value="Create" />
        </p>
    </fieldset>
}

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

@section Scripts {
    @Scripts.Render("~/bundles/jqueryval")
}

Basically, the problem is that each time I click on the “create” button, the ModelState validation is always false for the OtherObj even if something is selected in the dropdownlist. Every other values are correct except this one.

I don’t understand why and I would greatly appreciate your help on this one, thank you for your help!

After code edited **

Now I get a crash as I enter the “Create” view:

DataBinding: 'System.String' does not contain a property with the name 'OtherObj'.

Exactly on the line where my dropdownlistfor is located.

The datavalueField and dataTextField are supposed to refer to what exactly?

  • 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-06-19T03:50:24+00:00Added an answer on June 19, 2026 at 3:50 am

    add otherObjectId to your model

    public class ObjInfo
    {
        public int m_Id { get; set; }
    
        ...
    
        [Required(ErrorMessage = "Other Obj is required.")]
        [Display(Name = "OtherObj")]
        public int otherObjectId { get; set; }
    
        public OtherObjInfo m_OtherObj { get; set; }
    
        ...
    
    }
    

    controller

    public ActionResult Create()
    {
        ViewBag.List = new SelectList(PopulateDDLs(), "Id", "Name"); 
        return View();
    }
    
    [HttpPost]
    public ActionResult Create(Objinfo obj)
    {
        if (ModelState.IsValid)
        {
            m_ObjManager.CreateObj(obj);
            return RedirectToAction("SearchIndex");
        }
    
        ViewBag.List = new SelectList(PopulateDDLs(), "Id", "Name"); 
        return View(obj);
    }
    

    view

    <div class="editor-label">
        @Html.LabelFor(model => model.m_OtherObj , "Other Obj")
    </div>
    <div class="editor-field">
        @Html.DropDownListFor(model => model.otherObjectId, ViewBag.List as SelectList, "--- Select Category ---", new { @class = "some_class" })
        @Html.ValidationMessageFor(model => model.m_OtherObj)
    </div>
    

    better way is using strongly typed helpers. All your helpers are strongly-typed (editorfor, textboxfor, dropdownlistfor,…) except dropdownlist.

    if you want to bind DDL value to your model, You should use dropdownlistfor instead of dropdownlist.

    your model state is not valid, because you dont bind required value as DDL to model.

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

Sidebar

Related Questions

Note my code below. I am trying to figure out why my data is
Updated below . The following is the entire code I have in my main.cpp:
Note there's a lot of code posted below but its mostly all the same,
Note: This code actually codes from a tutorial book I'm reading at the moment,
Here is the code: //Note: x actually isn't defined, I'm pulling it from an
I have the following HTML code (note no closing tags on <\option> ) generated
I have such a basic problem in Delphi,I can't solve it. My Code: Note:DataR
System.Diagnostics.Contracts.ContractException is not accessible in my test project. Note this code is purely myself
let's say I have a html structure like this -> (note: in my code
Note that all the code is a simplified example in order to only communicate

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.