I have a MachineSettings
entity and this entity has a Machine
entity as a foreign key.
public class Machine
{
[Key]
public int MachineId { get; set; }
[Required]
[Display(Name = "Machine Type")]
public string MachineName { get; set; }
}
public class MachineSettings
{
[Key]
public int MachineSettingId { get; set; }
[Required]
public string Password { get; set; }
[Required]
public int ReferenceKeyLength { get; set; }
[Required]
public virtual Machine MachineId { get; set; }
}
In my controller class, when I created an Edit
method as POST
:
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Edit(MachineSettings machineSettings)
{
if (ModelState.IsValid)
{
db.Entry(objapp).State = EntityState.Modified;
db.SaveChanges();
return RedirectToAction("Index");
}
return View(machineSettings);
}
On the page I have shown the list of machines in drop down list, when do the changes and select another machine in dropdown and click on save. The Edit
method is called with a MachineSetting
object. Here my ModelState.IsValid
always false.
On checking the object in debug mode, I am getting the MachineId
from the drop down, but MachineName
is not returned, so how to avoid MachineName
checking in this so that ModelState.IsValid
property will be true?