Asp.net MVC UpdateModel(myClass) 的基本问题

发布于 2024-08-03 02:40:40 字数 1447 浏览 6 评论 0原文

在 Asp.net MVC 1 应用程序的控制器中,我想使用 UpdateModel 用控制器中的 POST 数据填充变量。我已经看过几十个例子,但即使是最基本的例子对我来说似乎也默默地失败了。

这是一个非常基本的示例,但不起作用。 我做错了什么?

    public class TestInfo
    {
        public string username;
        public string email;
    }

   public class AdminController : Controller
    {

        public ActionResult TestSubmit()
        {
            var test = new TestInfo();
            UpdateModel(test);//all the properties are still null after this executes  
            //TryUpdateModel(test); //this returns true but fields / properties all null
            return Json(test);
        }


    }


//Form Code that generates the POST data
    <form action="/Admin/TestSubmit" method="post">
        <div>
            <fieldset>
                <legend>Account Information</legend>
                <p>
                    <label for="username">Username:</label>
                    <input id="username" name="username" type="text" value="" />
                </p>
                <p>
                    <label for="email">Email:</label>
                    <input id="email" name="email" type="text" value="" />
                </p>
                <p>
                    <input type="submit" value="Login" />
                </p>

            </fieldset>
        </div>
    </form>

In my Controller in a Asp.net MVC 1 app I want to use UpdateModel to populate a variable with POST data in my controller. I've looked at dozens of examples but even the most basic ones seem to fail silently for me.

Here's a very basic example that's just not working.
What am I doing wrong?

    public class TestInfo
    {
        public string username;
        public string email;
    }

   public class AdminController : Controller
    {

        public ActionResult TestSubmit()
        {
            var test = new TestInfo();
            UpdateModel(test);//all the properties are still null after this executes  
            //TryUpdateModel(test); //this returns true but fields / properties all null
            return Json(test);
        }


    }


//Form Code that generates the POST data
    <form action="/Admin/TestSubmit" method="post">
        <div>
            <fieldset>
                <legend>Account Information</legend>
                <p>
                    <label for="username">Username:</label>
                    <input id="username" name="username" type="text" value="" />
                </p>
                <p>
                    <label for="email">Email:</label>
                    <input id="email" name="email" type="text" value="" />
                </p>
                <p>
                    <input type="submit" value="Login" />
                </p>

            </fieldset>
        </div>
    </form>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

爱冒险 2024-08-10 02:40:40

看起来您正在尝试让控制器根据表单元素更新模型。请尝试这样做:

    [AcceptVerbs(HttpVerbs.Post)]
    public ActionResult TestSubmit(TestInfo test)
    {
        UpdateModel(test);
        return Json(test);
    }

在您的代码中,您正在创建一个新的 TestModel,而不是让 MVC 运行时从 HttpPost 序列化它。我也让自己在这件事上陷入困境,你不是唯一的一个!

It looks like you're trying to get the controller to update the model based on the form elements. Try this instead:

    [AcceptVerbs(HttpVerbs.Post)]
    public ActionResult TestSubmit(TestInfo test)
    {
        UpdateModel(test);
        return Json(test);
    }

In your code, you're creating a new TestModel instead of letting the MVC runtime serialize it from the HttpPost. I've let myself get wrapped around the axel on this also, you're not the only one!

如梦亦如幻 2024-08-10 02:40:40

使您的公共领域具有以下属性:

    public class TestInfo
    {
        public string username {get;set;}
        public string email{get;set;}
    }

make properties of your public field:

    public class TestInfo
    {
        public string username {get;set;}
        public string email{get;set;}
    }
墨小沫ゞ 2024-08-10 02:40:40

我对 ASP.NET MVC 不太熟悉,但您的 TestSubmit 方法不应该看起来更像这样:

public ActionResult TestSubmit(TestInfo test)
{
  UpdateModel(test);
  return Json(test);
}

I'm not too familiar with ASP.NET MVC, but shouldn't your TestSubmit method look more like this:

public ActionResult TestSubmit(TestInfo test)
{
  UpdateModel(test);
  return Json(test);
}
末骤雨初歇 2024-08-10 02:40:40

在控制器中,您应该有两种方法,一种用于响应 GET,另一种(如果需要)用于响应 POST。

因此,首先有一个 GET 方法:

public ActionResult Test ()
{
    return View (/* add a TestInfo instance here if you're getting it from somewhere - db etc */);
}

其次,您需要一个 POST 方法:

[AcceptVerbs(HttpVerbs.Post)]
public ActionResult Test (TestInfo test)
{
    return Json (test);
}

请注意,那里没有 UpdateMethod ,即 ModelBinder 会为您完成此操作。

In the controller you should have two methods, one to respond to the GET, the other, if required is for responding to the POST.

So, firstly have a GET method:

public ActionResult Test ()
{
    return View (/* add a TestInfo instance here if you're getting it from somewhere - db etc */);
}

Secondly, you'll need a POST method:

[AcceptVerbs(HttpVerbs.Post)]
public ActionResult Test (TestInfo test)
{
    return Json (test);
}

Notice that there's no UpdateMethod there, the ModelBinder would have done that for you.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文