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

  • Home
  • SEARCH
  • 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 277953
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T01:09:43+00:00 2026-05-12T01:09:43+00:00

I would like to keep model binding when doing ajax calls from my view.

  • 0

I would like to keep model binding when doing ajax calls from my view. I’m using jquery ajax to make the calls, and just as an example- this is my controller method I want to call from ajax:

public ActionResult Create(Person personToCreate) {
    //Create person here
}

As you can see, this method is relying on Model Binding. This makes the method a lot cleaner… however, this means that when the ajax makes a call, it must supply all variables that are non-nullable in the DB.

So, if I have a table called Person that has variables:

firstName varchar(25) not-null
lastName  varchar(25) not-null
myPet     int         not-null <-- This is a foreign key

Then the Entity Framework class, Person, created would be similar to:

public class Person {
   public string firstName { get; set; }
   public string lastName { get; set; }
   public Pet myPet { get; set; }
}

Since none of the variables can be null (as specified in the DB) this means the ajax call must supply a string firstName, string lastName, Pet myPet. But javascript cannot supply the Pet…

So I only have two options (that I know of):

  1. Allow myPet to be null in the DB

  2. Create a “flatter” class that represents a Person that doesn’t require the pet object…

ie:

public class SimplePerson {
    public string firstName { get; set; }
    public string lastName { get; set; }
    public string myPetName { get; set; }
}

The problem with the first option is that it seems odd to have to modify the DB… definitely something wrong about it because it’s allowing things it shouldn’t be…

The problem with the second option is that I have a lot of classes and it seems extensive to be writing duplicate classes for each one just to avoid this… If I had 30 classes, that would be 30 duplicate classes that I would have to create in order to allow for model binding.

Can anyone think of any better options, or give reasoning on why one option would be better than the other?

  • 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-12T01:09:43+00:00Added an answer on May 12, 2026 at 1:09 am

    The real trick here is to separate your view model classes from the actual model classes. The view model classes tend to be a bit simpler, and you need to map them back into your actual model classes before doing real stuff with them. Tools like AutoMapper make this pretty much a snap though.

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

Sidebar

Ask A Question

Stats

  • Questions 209k
  • Answers 209k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer Solution was to delete the queues. I tried using the… May 12, 2026 at 9:44 pm
  • Editorial Team
    Editorial Team added an answer I suspect it's a namespace problem - you're not using… May 12, 2026 at 9:44 pm
  • Editorial Team
    Editorial Team added an answer I combination of the System.IO.FileSystemWatcher and System.Net.FtpWebRequest/FtpWebResponse classes. We need… May 12, 2026 at 9:44 pm

Related Questions

I have an entities model with a many to many relationship. For simiplicity, lets
I'm trying to re-implement an old Reversi board game I wrote with a bit
I have a C# Windows Forms application, whose prototype was created on SQL Server
I have an ActiveRecord model that I would like to convert to xml, but

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.