首页 新闻 赞助 找找看

mvc的问题

0
悬赏园豆:20 [已解决问题] 解决于 2015-11-23 14:54

我定义了一个student类,然后在cotroller中的Index方法中使用,将产生了拼接字符串赋给t,那么问题来了,每当我在view视图里把t用强制类型放入多行文本textaera的时候,运行时它都会报错,说

未将对象引用设置到对象的实例。

controller

public ActionResult Index()

{
return View();
}

[HttpPost]
public ActionResult Index(student t)
{
//student t = new student();
StringBuilder mystr = new StringBuilder();
mystr.Append("你的名字叫什么" +t.Name + ",是" );
mystr.Append(t.Sex + "生,");
mystr.Append("年龄断为" + t.Age+",");
mystr.Append("体型" + t.Body+",");
mystr.Append("爱好是" + t.Hobitty.First() + ",");
//if (t.Hobitty.h1 == "true") { mystr.Append("看书"); }
t.Message = mystr.ToString();
ViewData.Model = t;
return View(new student());
}

view视图

     <td>@Html.TextArea( Model.Message)</td>

student类

public class student
{

public string Name { set; get; }
public int Age { set; get; }
public char Sex { set; get; }

public string Body { set; get; }
public string Message { set; get; }
public string Image { set; get; }
public string[] Hobitty{ set; get; }
public student (string Name,int Age,char Sex,
string Body,string Message,
IEnumerable<string> Hobitty)
{

this.Name = Name;
this.Age = Age;
this.Sex = Sex;
this.Body = Body;
this.Message = Message;
List<string> ss = new List<string>(3);
foreach(var a in Hobitty)
{
ss.Add(a);
}
this.Hobitty= ss.ToArray() ;
}
public student() {}
}

hoofin的主页 hoofin | 初学一级 | 园豆:4
提问于:2015-11-18 09:52
< >
分享
最佳答案
0

问题是这样的。

[HttpPost]
public ActionResult Index(student t){return View(new student());}//定义是需要post请求的时候才会执行 

public ActionResult Index(){return View();}//直接访问

视图

     <td>@Html.TextArea( Model.Message)</td>

如果是直接访问就会报错。因为没有view方法没有传入model对象。直接取就会是空引用。

使用post访问就不会报空引用错误了。

//建议更改为

[HttpPost]
public ActionResult Index(student t){

//student t = new student();

------原有代码-------

return View(t);

}

public ActionResult Index(){return View(new student());}

收获园豆:20
凝冰 | 小虾三级 |园豆:685 | 2015-11-18 10:52

传入参数t之后,不能再实例化,它会报错

hoofin | 园豆:4 (初学一级) | 2015-11-18 12:05

@hoofin: 

控制器:

public ActionResult Index()
        {
            return View(new student());
        }

        [HttpPost]
        public ActionResult Index(student t)
        {
            StringBuilder mystr = new StringBuilder();
            mystr.Append("你的名字叫什么" + t.Name + ",是");
            mystr.Append(t.Sex + "生,");
            mystr.Append("年龄断为" + t.Age + ",");
            mystr.Append("体型" + t.Body + ",");
            
            mystr.Append("爱好是," );
            if (t.Hobitty != null)
            {
                foreach (var item in t.Hobitty)
                {
                    mystr.Append(item);
                }
            }
            mystr.Append(",");
            //if (t.Hobitty.h1 == "true") { mystr.Append("看书"); }
            t.Message = mystr.ToString();
            ViewData.Model = t;
            return View(t);
        }

view

@model MvcApplication3.Controllers.student
@{
    ViewBag.Title = "Index";
}
<h2><td>
    @Html.TextArea("areaName", Model.Message)
    </td>
</h2>
<div>
    <form action="~/Home" id="form1" method="post">
        <input name="Name" value="张三" type="hidden" />
        <input name="Sex" value="" type="hidden" />
        <input name="Body" value="偏胖" type="hidden" />
        <input name="Hobitty" value="体育" type="hidden" />
        <input name="Hobitty" value="纸牌" type="hidden" />
        <input type="submit"/>
    </form>
</div>

提交前:

提交后

说明一下:

@Html.TextArea("文本域name属性的值","默认值");

例如@Html.TextArea("test","默认值");

生成html:<textarea name="test" id="text" rows="2" cols="20">默认值</textarea>

凝冰 | 园豆:685 (小虾三级) | 2015-11-18 12:56

@凝冰: 好,我试试 能加个QQm吗?

hoofin | 园豆:4 (初学一级) | 2015-11-18 13:02

@hoofin: 546341921

凝冰 | 园豆:685 (小虾三级) | 2015-11-18 13:03

@凝冰: 还是不行,能帮我调试一下吗?

hoofin | 园豆:4 (初学一级) | 2015-11-18 13:05
其他回答(1)
0

从报错上你就能了解到是空异常,你写了两个index的action,你第一个是不在参数的,第二个是请求带实体类的,首先你要区分你在请求的是哪个action,另外你的ViewData.Model = t;
return View(new student());这两行本来就冲突,因为他们是等效的。

流年莫逝 | 园豆:247 (菜鸟二级) | 2015-11-18 10:03

我想在一个页面上发送请求和接受,

支持(0) 反对(0) hoofin | 园豆:4 (初学一级) | 2015-11-18 10:05

@hoofin:我知道,在一个页面上做这些处理是可以的,关键你报错看看是在哪个请求上出的错

支持(0) 反对(0) 流年莫逝 | 园豆:247 (菜鸟二级) | 2015-11-18 10:07

@流年莫逝:在用httppost强类型传到view多行文本的时候报的错

支持(0) 反对(0) hoofin | 园豆:4 (初学一级) | 2015-11-18 10:10
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册