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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T02:52:18+00:00 2026-06-09T02:52:18+00:00

I have a question about ASP.NET MVC3 model binding. If I have a class

  • 0

I have a question about ASP.NET MVC3 model binding. If I have a class I’m trying to use as a model, but I don’t want the key put on the page, the model doesn’t bind on a POST. Here is an example:

//Data Model
public class MyModel
{
    [Key]
    public string MyKey {get;set;} //Perhaps this is an ssn that I don't want on the form.
    public string MyValueToGet {get;set;} //This is the value I want the user to enter.
}

//Conroller code.
public ViewResult Index()
{
    MyModel model = new MyModel{ MyKey = "SecretInfo", MyValueToGet = "" };
    return View(new model);
}
public ActionResult Edit(MyModel model)
{
    repository.SaveChanges(model)
}

//View code.
@using(Html.BeginForm("Edit", "Home", FormMethod.Post))
{
    Enter a value: @Html.EditorFor(m => m.MyValueToGet)
    <input type="submit" value="Salve" />
}

So my problem is that model is null when the Edit method is called upon form submission. I can fix this by putting MyKey somewhere on the page (perhaps as a hidden field), but that is unacceptable if it is some sort of sensitive data. Is there a way to solve this problem? I am new to MVC, so I appreciate any help.

  • 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-09T02:52:20+00:00Added an answer on June 9, 2026 at 2:52 am

    Create another unique but otherwise meaningless identifier like an (auto increment int) and use that to bind.

    in other words modify your model to something like:

    public class MyModel
    {
        [Key]
        public int ID {get; set;}
        public string MyKey {get;set;} //Now this can be sensitive, it doesn't matter because you no longer rely on it.
        public string MyValueToGet {get;set;} //This is the value I want the user to enter.
    }
    

    EDIT

    I believe your best choice would be to change the MyModel object, as it’s design is flawed. The primary key in the majority of cases (and I think this is one of them) should be a simple auto incrementing integer, meaningless apart from it’s role as the table’s key.

    While Luke’s suggestion to use Session is a viable option and a solution that would work, I would personally do something similar to what I’ll explain here, as it would seem to me to be more of the ‘mvc way’ of doing things.

    Data model:
    Either change your current model to something like what I suggest above, or, if that is not feasible for whatever reason (breaking dependancies or FK relationships), create a new table that can be used as a join, or proxy, if you will:

    public class Proxy
    {
        public int ProxyId {get;set;}
        public MyModel MyModel {get; set;}
    }
    

    Obviously, you’d have to do some work to populate this table, but you would then be able to use it to fetch records from MyModel without accessing the MyKey property directly.

    It’s not considered good practice to use your data models directly in your views, so you want to create a view model as well

    public class MyModelViewModel
    {
        public int ModelId {get; set;}
        public string ModelValueToGet {get; set;}
    }
    

    Notice we don’t even need the key containing sensitive data in the view model.

    Then type your view to the viewModel, not the data model, and include a hidden field for the ModelId

    @using(Html.BeginForm("Edit", "Home", FormMethod.Post))
    {
        Enter a value: @Html.EditorFor(m => m.ModelValueToGet)
        @Html.HiddenFor(m => m.ModelId)
        <input type="submit" value="Save" />
    }
    

    Now in your controller you have your get method

    public ViewResult Index()
    {
        //fetch the users record from the database 
        //if you're using the Proxy table, you'll want to write a LINQ query here
        //instantiate a viewModel and populate it's properties using the fetched record
        //remember, the viewModel.ModelId should be set to MyModel.ID or Proxy.ProxyId 
        //render the view
    }
    

    And the post method

    public ViewResult Edit (MyModelViewModel viewModel)
    {
        //fetch the users record from the database using viewModel.ModelId
        //If you're using the proxy table, you'll need to use that LINQ query again here
        //update the record you fetched with the new data the user just entered
        //you have complete control here of what gets updated and what stays the same
        //pass the updated record to the repository to save the changes.
        //redirect the user to be on their merry way
    }
    

    I think that’s about as well as I can lay it out. Hope it makes sense.

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

Sidebar

Related Questions

i have a question about modelstate in asp.net mvc3 that , i have a
I'm a newbie about ASP.NET MVC 3, but I have a simple question. Is
I have a question about how ASP.NET AJAX partial rendering actually works. Does it:
I have a question about using form authentication in ASP.net MVC. I ran aspnet_regsql
I have a question about viewstate and custom controls in asp.net. Say I have
I have a question about the returnUrl querystring parameter that is appended by ASP.Net
I'm writing my first asp.net mvc application and I have a question about custom
Have a question about the design/usage of asp.net mvc here. In the html helper
I have a question about complex views in ASP.NET MVC. Where can I find
I have a question about the web.config in a asp.net website. For a project

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.