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 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
  • 1 View
  • 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

Related Questions

I am teaching my friend how to make websites. I would like to keep
Question: How do send data to a view model when using command binding? So
I would like to keep the overhead at a minimum. Right now I have:
I would like to keep a static counter in a garbage collected class and
I would like to keep a list of a certain class of objects in
I currently have a controller were I would like to keep track of logging
Would like to get a list of advantages and disadvantages of using Stored Procedures.
I would like to sort an array in ascending order using C/C++ . The
I would like to have a reference for the pros and cons of using
I have a JTable which is loaded from a data-structure using table model.The data-structure

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.