C# “/”应用程序中的服务器错误

asp.net MVC练手 出错及解决全过程


1.在Models文件中新建类Person.cs

   public class Person
    {
        public string Name { get; set; }
        public string Email { get; set; }
        public string Phone { get; set; }
        public bool? Gender { get; set; }
    }

2.新建立一个文件夹名字叫 Action, 里面添加一个页面PersonIndex.cshtml

@using (Html.BeginForm("SendInformation", "Home"))//(actionName,controllerName)
    {
        <p>Your Name:@Html.TextBoxFor(x=>x.Name)</p>
        <p>Your Email:@Html.TextBoxFor(x => x.Email)</p>
        <p>Your Phone:@Html.TextBoxFor(x => x.Phone)</p>
        <p>
            Gender:
            @Html.DropDownListFor(x=>x.Gender,
                new[] { new SelectListItem(){ Text="I am a boy",Selected=true ,Value ="male"},
                        new SelectListItem () {Text="I am a girl",Selected =false ,Value ="female" }
                      },"please select your gender"
            )
        </p>
        <p>
            <input type="submit" value="Submit your information" />
        </p>
    }

3.添加超链接到Home 的Index.cshtml页面

@Html.ActionLink("个人信息", "PersonInformation", "Home", routeValues:null,htmlAttributes:new {id="personIndexLink" })

4.在Controller的HomeController.cs中新增方法:

       public string SendInformation(Person person)
        {
            return "Thank you," + person.Name;
        }

        public ActionResult PersonInformation()
        {
            return View("PersonIndex");
        }


一切就绪,运行index.cdhtml页面,点击“个人信息”超链接,保存如下:

“/”应用程序中的服务器错误。

未找到视图“PersonIndex”或其母版视图,或没有视图引擎支持搜索的位置。搜索了以下位置:
~/Views/Home/PersonIndex.aspx
~/Views/Home/PersonIndex.ascx
~/Views/Shared/PersonIndex.aspx
~/Views/Shared/PersonIndex.ascx
~/Views/Home/PersonIndex.cshtml
~/Views/Home/PersonIndex.vbhtml
~/Views/Shared/PersonIndex.cshtml
~/Views/Shared/PersonIndex.vbhtml

不搜索我的视图中的Action文件夹,根本就找不到PersonIndex.cshtml.修改方案往下看:

方案一:将PersonIndex.cshtml页面挪到Home文件夹中

方案二:

1,第一步没错,保留

2,第二步,将视图中PersonIndex.cshtml所在文件夹改为PersonAction

@using (Html.BeginForm("SendInformation", "PersonAction"))//(actionName,controllerName)
    {
        <p>Your Name:@Html.TextBoxFor(x=>x.Name)</p>
        <p>Your Email:@Html.TextBoxFor(x => x.Email)</p>
        <p>Your Phone:@Html.TextBoxFor(x => x.Phone)</p>
        <p>
            Gender:
            @Html.DropDownListFor(x=>x.Gender,
                new[] { new SelectListItem(){ Text="I am a boy",Selected=true ,Value ="male"},
                        new SelectListItem () {Text="I am a girl",Selected =false ,Value ="female" }
                      },"please select your gender"
            )
        </p>
        <p>
            <input type="submit" value="Submit your information" />
        </p>
    }

3,第三步,@Html.ActionLink("个人信息", "PersonInformation", "PersonAction", routeValues:null,htmlAttributes:new {id="personIndexLink" })

4,第四步,在Controller文件中新增PersonActionController.cs,将上面第四步的代码挪过来,结构如下:

其实就是遵循一个原则,名字相同。


方案一 是名字都为Home,方案二是都为PersonAction,我是初学者,原理我还不太懂。

哪位大哥大姐愿意指教,这里不胜感激!!!

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值