I created an editor template to display text boxes included in a form.
@model WebApplication1.Models.PersonViewModel
<input type="text" class="form-control" name="@ViewData.TemplateInfo.HtmlFieldPrefix" value=""/>
I simply want to include the 'form-control' class in all my input text boxes used to collect data for this model.
public class PersonViewModel
{
[UIHint("_TextFormControl")]
public string FirstName { get; set; }
[UIHint("_TextFormControl")]
public string LastName { get; set; }
public int Age { get; set; }
}
This works great when the form starts out blank, however, when I use the template for forms pre-populated with data, like when I want to edit an existing model, I get the error message: "The model item passed into the dictionary is of type 'System.String', but this dictionary requires a model item of type 'WebApplication1.Models.PersonViewModel'."
Here is my controller:
// GET: /Person/Edit/5
[HttpGet]
public ActionResult Edit(int? id)
{
if (id == null)
{
return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
}
Person person = db.Persons.Find(id);
if (person == null)
{
return HttpNotFound();
}
return View(person);
}
How do I allow this editor to be used with existing data, as well as new forms?