0

I have a Student class in my application as below:

public class Student
{
    public int ID { get; set; }
    public string Name { get; set; }
    public string RoleNum { get; set; }
    public DateTime RegistrationDate { get; set; }
    public DateTime AdmissionDate { get; set; }
}

Now I have a few views in the application which update the student model. But not every view needs to update every field of the student table in database. e.g. the registration date is set only once when the student is first created. Now the edit student view should not again update the RegistrationDate.

The problem is that RegistrationDate is the required field in the database so not including that in the view form generates the exception of having NULL in RegistrationDate.

So to prevent that, I am hiding the RegistrationDate fields in a div so it is not visible in the form. Is that the right way of doing this thing or am I missing a very simple way?

Akif Tariq
  • 71
  • 1
  • 9

2 Answers2

0

Instead of hiding them... just make them Hidden Field:

@Html.HiddenFor(model => model.RegistrationDate)

Or the other option is just use a Hidden for the Student ID, and once you post data to the server (the RegistrationDate will be null)... you can get the Student from the database and fill the values you have null.

The second option is a bit more secure, as the user cannot alter the registration date in the hidden on client side.

Please check this question, very well explained.

Community
  • 1
  • 1
Romias
  • 13,783
  • 7
  • 56
  • 85
0

When updating an entity the idea is to first fetch the entity from the database that that you want to update using the ID and then use the TryUpdateModel method to update only the fields that were part of the original request and finally save the model.

Here's a commonly used pattern for updating an entity:

[HttpPost]
public ActionResult Update(int id)
{
    Student student = Repository.GetStudent(id);
    if (!TryUpdateModel(student))
    {
        // there were validation errors => redisplay the view
        return View(student);
    }

    // the model is valid => at this stage we could save it
    Repository.Update(student);
    return RedirectToAction("success");
}
Darin Dimitrov
  • 1,023,142
  • 271
  • 3,287
  • 2,928
  • @Romias, the data is POSTed from the HTML form that you will have in the view. The fact that I have used only the ID property as argument doesn't mean that other values won't be present. The `TryUpdateModel` uses all values that were present in the Request. So all you have to do is to include the fields you are interested in the form. – Darin Dimitrov Mar 22 '12 at 21:36
  • Ok, so the TryUpdateModel is a kind of more intelligent binder? – Romias Mar 22 '12 at 21:38
  • @Romias, no, it is not a kind of a more intelligent binder. It is the model binder itself. That's what it uses behind the scenes. In fact it is the same if you had followed good practices and have defined a view model that would contain only some properties and used this view model as action argument. – Darin Dimitrov Mar 22 '12 at 21:39