I am trying to figure out how to shorten my code but I'm stuck on this issue, please take a look at this.
I have a form like
@using (Html.BeginUmbracoForm("Save", "Student", FormMethod.Post))
{
<input type="hidden" name="Id" />
<input type="text" name="Name" />
<input type="text" name="NickName" />
<input type="text" name="Age" />
<input type="text" name="Address" />
<input type="submit" />
}
while here is my controller
public ActionResult Save(Student s) {
var record = _db.Students.Find(s.Id);
if(record != null) {
record.Id = s.Id;
record.Name = s.Name;
record.NickName = s.NickName;
record.Age = s.Age;
record.Address = s.Address;
_db.SaveChanges();
}
return RedirectToCurrentUmbracoUrl():
}
and finally my Model
public class Student() {
public int Id { get; set; }
public string Name { get; set; }
public string NickName { get; set; }
public int Age { get; set; }
public int Address { get; set }
}
Alright, above code works fine, no problem on it.
My Problem
I have different views, where I only update some of the Student's class properties, and I want to use the same Controller. See my 3 forms below
@using (Html.BeginUmbracoForm("Save", "Student", FormMethod.Post))
{
<input type="hidden" name="Id" />
<input type="text" name="Name" />
<input type="submit" />
}
@using (Html.BeginUmbracoForm("Save", "Student", FormMethod.Post))
{
<input type="hidden" name="Id" />
<input type="text" name="Age" />
<input type="submit" />
}
@using (Html.BeginUmbracoForm("Save", "Student", FormMethod.Post))
{
<input type="hidden" name="Id" />
<input type="text" name="Address" />
<input type="submit" />
}
Now the problem is that if I submit the first form, then the Student Age and Student Address will contain null or empty because they have no values.
So how can I do it without creating three Controllers like:
public ActionResult SaveStudentName(),public ActionResult SaveStudentAge()andpublic ActionResult SaveStudentAddress()
that save specific Student detail ?
I only want to use the controller public ActionResult Save() where all actions related to saving Student detail must forward here.
Sorry for my bad english, I hope somebody can give me a tip.