I have encountered the error shown as my title. I have tried to search for solutions but all I got is solution about using try catch
code blocks.
I have been using a course documentation that I have made to guide me on doing this project but the error that I have encountered this time, I am clueless about which part has gone wrong and how to check the wrong part.
There are two parts that I have commented it with // strange
comments which means that I have no idea is it where the error occur or something like that.
Thanks for reading my question.
This is my PetRescued Model
public class PetRescued
{
public int Id { get; set; }
[Required]
[StringLength(255)]
public string PetName { get; set; }
public int PetAge { get; set; }
[Required]
[StringLength(6)]
public string PetGender { get; set; }
public short PetWeightInKg { get; set; }
public DateTime DateWhenRescued { get; set; }
public PetSpecies PetSpecies { get; set; }
public byte PetSpeciesId { get; set; }
}
This is my PetRescued Controller
public ActionResult New() //populate form
{
var petspecies = _context.PetSpecieses.ToList();
var viewModel = new PetRescuedViewModel
{
PetSpecies = petspecies
};
return View("PetRescued", viewModel);
}
[HttpPost]
public ActionResult Save(PetRescued petRescued)
{
if (petRescued.Id == 0)
_context.PetRescueds.Add(petRescued);
else
{
var petRescuedInDb = _context.PetRescueds.Single(c => c.Id == petRescued.Id);
petRescuedInDb.PetName = petRescued.PetName;
petRescuedInDb.PetAge = petRescued.PetAge;
petRescuedInDb.PetGender = petRescued.PetGender;
petRescuedInDb.PetWeightInKg = petRescued.PetWeightInKg;
petRescuedInDb.PetSpeciesId = petRescued.PetSpeciesId; //strange
petRescuedInDb.DateWhenRescued = petRescued.DateWhenRescued;
}
_context.SaveChanges();
return RedirectToAction("Index", "PetRescued");
}
This is my PetRescued ViewModel
public class PetRescuedViewModel
{
public IEnumerable<PetSpecies> PetSpecies { get; set; }
public PetRescued PetRescueds { get; set; }
public PetRescuedViewModel()
{
PetRescueds = new PetRescued();
}
}
This is my PetRescued Form
@using (Html.BeginForm("Save", "PetRescued"))
{
<div class="form-group">
@Html.LabelFor(m => m.PetRescueds.PetName)
@Html.TextBoxFor(m => m.PetRescueds.PetName, new { @class = "form-control" })
</div>
//strange
<div class="form-group">
@Html.LabelFor(m => m.PetSpecies)
@Html.DropDownListFor(m => m.PetRescueds.PetSpeciesId, new SelectList(Model.PetSpecies, "Id", "SpeciesName"), "Select A Species", new {@class = "form-control"})
</div>
<div class="form-group">
@Html.LabelFor(m => m.PetRescueds.PetAge)
@Html.TextBoxFor(m => m.PetRescueds.PetAge, new { @class = "form-control" })
</div>
<div class="form-group">
@Html.LabelFor(m => m.PetRescueds.PetGender)
@Html.TextBoxFor(m => m.PetRescueds.PetGender, new { @class = "form-control" })
</div>
<div class="form-group">
@Html.LabelFor(m => m.PetRescueds.PetWeightInKg)
@Html.TextBoxFor(m => m.PetRescueds.PetWeightInKg, new { @class = "form-control" })
</div>
<div class="form-group">
@Html.LabelFor(m => m.PetRescueds.DateWhenRescued)
@Html.TextBoxFor(m => m.PetRescueds.DateWhenRescued, "{0:d MMM yyyy}", new { @class = "form-control" })
</div>
@Html.HiddenFor(m => m.PetRescueds.Id)
<button type="submit" class="btn btn-primary">Save</button>
}