Reputation: 11735
In my controller I am having the following code
public ActionResult Tests(int clientID, string clientName)
{
TestModel model = new TestModel (clientID, clientName);
return View(model);
}
[HttpPost]
public ActionResult Tests(TestModel model)
{
try
{
model.Save(model);
return GetClientView();
}
catch (Exception e)
{
ModelState.AddModelError("Error", e.Message);
return RedirectToAction("Tests");
}
}
In my MyPage.cshtml I have
@using (Html.BeginForm())
{
<h2> @Model.ClientName</h2>
...
<p> <input type ="submit" value="Save" id="submit" />
</p>
}
However, upon clicking on the submit button, I am having the error message
No parameterless constructor defined for this object.
Exception Details: System.MissingMethodException: No parameterless constructor defined for this object.
The page source shows the following
<form action="/Client/Tests?clientID=891&clientName=Another%20Client" method="post">
What am I doing wrong?
Upvotes: 0
Views: 313
Reputation: 9676
Try creating a parameterless constructor in your TestModel
:
public class TestModel
{
public TestModel()
{
}
}
That should do the trick!
Upvotes: 3
Reputation: 1182
You do not seem to insert or update your model before the save.
With Entity Framework
Update:
Try
{
TryUpdateModel(model);
model.Save(model);
return GetClientView();
}
Insert:
Try
{
AddToTable(model); // Table being your Entity Model's entity type name
model.Save(model);
return GetClientView();
}
Upvotes: 0
Reputation: 6851
Try to use:
using (Html.BeginForm("Tests", "YourController")
{
<div id="create-event">
<div>
@Html.LabelFor(model => model.ClientId)
@Html.TextBoxFor(model => model.ClientId)
</div>
<div>
@Html.LabelFor(model => model.ClientName)
@Html.TextBoxFor(model => model.ClientName)
</div>
<input type="submit" value="Ok" />
</div>
}
Upvotes: 0