MVC空模型问题

发布于 2024-09-04 17:59:41 字数 214 浏览 4 评论 0原文

我创建了两个创建操作..一个用于调用创建视图,另一个用于使用 httppost 处理创建视图。

当我调用创建视图时,它会正确发布,下拉列表等等。 问题是,当我填写创建表单并单击提交按钮时,出现错误;

你调用的对象是空的。

我的第一个想法是我将空模型传递给 httppost 创建操作。 我如何检查是否将空模型传递给 httppost 创建操作?

谢谢

i have created two create actions..one to call the create view and the other to process the create view using httppost.

when i call the create view, it gets published correctly , dropdowns and all.
the problem is that when i fill out the create form and click on the submit button, i get an error;

Object reference not set to an instance of an object.

My first thoughts are that i am passing a null model to the httppost create action..
How can i check to see if i am passing in a null model to the httppost create action?

thanks

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

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

发布评论

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

评论(1

枫以 2024-09-11 17:59:41

您到底在哪里得到这个异常?是在控制器操作中还是在渲染视图时?通常的范例如下:

public ActionResult New()
{
    // as you will be creating a new entity you don't need to pass
    // any model here unless your view depends on some property of the model
    return View();
}

[HttpPost]
public ActionResult Create(SomeModel model)
{
    // The model parameter here will be automatically instantiated 
    // by the default model binder and its properties will be 
    // initialized to the values entered by the user in the view
    if (ModelState.IsValid)
    {
        // save the entity
        Repository.Save(model);
        // redirect back to the index action
        return RedirectToAction("Index");
    }
    // validation failed => pass the model to the view in order to
    // preserve values and show validation errors
    return View("New", model);
}

Where exactly you are getting this exception? Is it in the controller action or while rendering the view? The usual paradigm is the following:

public ActionResult New()
{
    // as you will be creating a new entity you don't need to pass
    // any model here unless your view depends on some property of the model
    return View();
}

[HttpPost]
public ActionResult Create(SomeModel model)
{
    // The model parameter here will be automatically instantiated 
    // by the default model binder and its properties will be 
    // initialized to the values entered by the user in the view
    if (ModelState.IsValid)
    {
        // save the entity
        Repository.Save(model);
        // redirect back to the index action
        return RedirectToAction("Index");
    }
    // validation failed => pass the model to the view in order to
    // preserve values and show validation errors
    return View("New", model);
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文