MVC架构错误 [英] mvc architechture errior

查看:86
本文介绍了MVC架构错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在做mvc项目,因为我已经通过视图传递了客户的详细信息,但是发现错误对象引用未设置为对象的实例".我的controller.cs页面看起来像...

i have doing mvc project in that i have pass dynemically customer details with view my but in that i have found error "Object reference not set to an instance of an object." my controller.cs page is look like...

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using customerdetails.Models;

namespace customerdetails.Controllers
{
    public class datacustomerController : Controller
    {
        //
        // GET: /datacustomer/

        public ActionResult Index()
        {

            return View();
        }
        public ActionResult FillCustomer()
        {
            return View();
        }
        public ActionResult DisplayCustomer()
        {
            customerdata obj=new customerdata();
            obj.Id=Convert.ToInt16(Request.Form["CustomerId"]);
            obj.CustomerCode=Request.Form["CustomerCode"].ToString();
            obj.Name = Request.Form["Customer Name"].ToString();
            obj.Amount=Convert.ToDouble(Request.Form["Amount"]);
            return View(obj);
        }
    }
}


推荐答案

这很可能是因为您的一个表单变量为null

This is most likely because one of your form variables is null

customerdata obj=new customerdata();
obj.Id = Convert.ToInt16(Request.Form["CustomerId"]);
obj.CustomerCode = Request.Form["CustomerCode"].ToString();
obj.Name = Request.Form["Customer Name"].ToString();
obj.Amount = Convert.ToDouble(Request.Form["Amount"]);
return View(obj);


此错误消息将使您指向代码中的一行.猜出它的Request.Form["Customer Name"](是错字?应该是CustomerName)


如果variable为null,则Convert.ToInt16(variable)不会为您提供null引用异常.
也不是Convert.ToDouble(variable).
因此,您可以使用Convert.Tostring(variable),如果variable为空,它将返回string.empty.这比检查每个Request.Form是否为空要容易得多


This errormessage will point you to a line in your code. A guess its the Request.Form["Customer Name"] (it''s a typo? should be CustomerName)


Convert.ToInt16(variable) wont give you an null reference exception if variable is null.
Not Convert.ToDouble(variable) either.
So you could use a Convert.Tostring(variable) this will return string.empty if variable is null. That is much easier than check every Request.Form for nulls



通常,当我们尝试指向或转换空值时,会发生此异常.
在您的情况下,"Request.Form"为null或其中存储的某些值为null.
您只需要查看您的代码即可.使用这个:
Hi,
Generally this exception occurs when we tried to point or convert a null value.
In your case "Request.Form" is null or some of the values stored in it, is null.
You just need to review your code for that. Use this:
public ActionResult DisplayCustomer()
{
    customerdata obj=new customerdata();
    if(Request.Form["CustomerId"] != null)
        obj.Id=Convert.ToInt16(Request.Form["CustomerId"]);
    if(Request.Form["CustomerCode"] != null)
        obj.CustomerCode=Request.Form["CustomerCode"].ToString();
    if(Request.Form["Customer Name"] != null)
        obj.Name = Request.Form["Customer Name"].ToString();
    if(Request.Form["Amount"] != null)
        obj.Amount=Convert.ToDouble(Request.Form["Amount"]);
    return View(obj);
}



祝一切顺利.
--Amit



All the best.
--Amit


这篇关于MVC架构错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆