ASP MVC 编辑帖子函数具有空参数

发布于 2024-11-11 16:39:17 字数 1304 浏览 0 评论 0原文

我正在尝试根据教程创建一个非常基本的 MVC 应用程序。我使用默认路由以及简单的视图和模型。

我遇到的问题是 HttpPost Edit 功能。我期望将“MyObject”类型的对象作为参数传递,但它总是返回 null。

以下是我在控制器中的编辑功能(获取功能正常工作):

public ActionResult Edit(int? id)
{
    if (!id.HasValue)
        return RedirectToAction("Index");

    var item = (from obj in mDB.MyDatabaseObjects
                where obj.Id == id
                select obj).First();
    return View(item);
}

//
// POST: /Main/Edit/5

[HttpPost]
public ActionResult Edit(MyDatabaseObject someObject)
{
    var original = (from obj in mDB.MyDatabaseObjects
                    where obj.Id == someObject.Id
                    select obj).First();

    if (!ModelState.IsValid)
        return View(original);

    mDB.ApplyCurrentValues(original.EntityKey.EntitySetName, someObject);
    mDB.SaveChanges();

    return RedirectToAction("Index");
}

请注意,我的(几乎相同的)创建方法按预期工作:

[HttpPost]
public ActionResult Create([Bind(Exclude="Id")] MyDatabaseObject newObject)
{
    if (!ModelState.IsValid)
        return View();

    int max = mDB.MyDatabaseObjects.Max(data => data.TaskOrder);

    newObject.TaskOrder = max + 1;
    mDB.AddToMyDatabaseObjects(newObject);
    mDB.SaveChanges();

    return RedirectToAction("Index");
}

谢谢,
工作时间

I'm trying to create a very basic MVC app based on a tutorial. I am using the default routing, and simple Views and Model.

The problem I am having is with the HttpPost Edit function. I am expecting an object of my "MyObject" type to be passed as the parameter, but it always comes back null.

Here are my Edit functions from the controller (the Get function works properly):

public ActionResult Edit(int? id)
{
    if (!id.HasValue)
        return RedirectToAction("Index");

    var item = (from obj in mDB.MyDatabaseObjects
                where obj.Id == id
                select obj).First();
    return View(item);
}

//
// POST: /Main/Edit/5

[HttpPost]
public ActionResult Edit(MyDatabaseObject someObject)
{
    var original = (from obj in mDB.MyDatabaseObjects
                    where obj.Id == someObject.Id
                    select obj).First();

    if (!ModelState.IsValid)
        return View(original);

    mDB.ApplyCurrentValues(original.EntityKey.EntitySetName, someObject);
    mDB.SaveChanges();

    return RedirectToAction("Index");
}

Note that my (nearly identical) Create method works as expected:

[HttpPost]
public ActionResult Create([Bind(Exclude="Id")] MyDatabaseObject newObject)
{
    if (!ModelState.IsValid)
        return View();

    int max = mDB.MyDatabaseObjects.Max(data => data.TaskOrder);

    newObject.TaskOrder = max + 1;
    mDB.AddToMyDatabaseObjects(newObject);
    mDB.SaveChanges();

    return RedirectToAction("Index");
}

Thanks,
wTs

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

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

发布评论

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

评论(1

不必在意 2024-11-18 16:39:17

确保 MyDatabaseObject 视图上的值位于表单内。验证这些值是否已发布 - 检查 Request.Form 或使用更改要使用的方法签名

FormsCollection collection

simply to validate the values are getting posted. If its choosing that method - it should be matching the properties to the form field - its generally very simple.

Ensure the values on your view for MyDatabaseObject are inside of the form. Validate these values are being posted over - inspect Request.Form or use change the method signature to use

FormsCollection collection

simply to validate the values are getting posted. If its choosing that method - it should be matching the properties to the form field - its generally very simple.

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