Asp.net mvc 由于参数不同,后处理困难

Asp.net mvc 由于参数不同,后处理困难,asp.net-mvc,asp.net-mvc-3,Asp.net Mvc,Asp.net Mvc 3,在我的控制器中,我有以下代码 public ActionResult Tests(int clientID, string clientName) { TestModel model = new TestModel (clientID, clientName); return View(model); } [HttpPost] public ActionResult Tests(TestModel model) {

在我的控制器中,我有以下代码

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");
        }
    }
在MyPage.cshtml中,我有

 @using (Html.BeginForm())
{
<h2> @Model.ClientName</h2>

...
<p> <input type ="submit"  value="Save" id="submit" />
     </p>
}
@使用(Html.BeginForm())
{
@Model.ClientName
...

}
但是,单击submit按钮时,我收到了错误消息

没有为此对象定义无参数构造函数

例外情况详情: System.MissingMethodException:否 为定义的无参数构造函数 这个物体

页面源代码显示以下内容

<form action="/Client/Tests?clientID=891&amp;clientName=Another%20Client" method="post">

我做错了什么?

尝试使用:

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>
    }
使用(Html.BeginForm(“Tests”、“YourController”)
{
@LabelFor(model=>model.ClientId)
@Html.TextBoxFor(model=>model.ClientId)
@LabelFor(model=>model.ClientName)
@Html.TextBoxFor(model=>model.ClientName)
}
尝试使用:

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>
    }
使用(Html.BeginForm(“Tests”、“YourController”)
{
@LabelFor(model=>model.ClientId)
@Html.TextBoxFor(model=>model.ClientId)
@LabelFor(model=>model.ClientName)
@Html.TextBoxFor(model=>model.ClientName)
}

在保存之前,您似乎没有插入或更新模型

具有实体框架

更新:

Try
{
         TryUpdateModel(model);
         model.Save(model);
         return GetClientView();
} 
插入:

Try
{
        AddToTable(model); // Table being your Entity Model's entity type name
        model.Save(model);
        return GetClientView();
}

您似乎没有在保存之前插入或更新模型

具有实体框架

更新:

Try
{
         TryUpdateModel(model);
         model.Save(model);
         return GetClientView();
} 
插入:

Try
{
        AddToTable(model); // Table being your Entity Model's entity type name
        model.Save(model);
        return GetClientView();
}

尝试在
TestModel
中创建无参数构造函数:

public class TestModel
{
    public TestModel()
    {
    }
}

这就可以了!

试着在您的
测试模型中创建一个无参数构造函数:

public class TestModel
{
    public TestModel()
    {
    }
}

这应该就够了!

我仍然有同样的错误,它甚至没有达到帖子的操作。我仍然有同样的错误,它甚至没有达到帖子的操作