您的位置:首页 > 编程语言 > C#

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

2016-05-27 14:49 537 查看
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,我是初学者,原理我还不太懂。

哪位大哥大姐愿意指教,这里不胜感激!!!
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: