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”或其母版视图,或没有视图引擎支持搜索的位置。搜索了以下位置:
不搜索我的视图中的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,我是初学者,原理我还不太懂。
哪位大哥大姐愿意指教,这里不胜感激!!!
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,我是初学者,原理我还不太懂。
哪位大哥大姐愿意指教,这里不胜感激!!!
相关文章推荐
- c#获取当前路径的方法
- c# 多线程 --Mutex(互斥锁)
- c# 时间操作
- C#使用yield关键字让自定义集合实现foreach遍历的方法
- C#做应用啦---打字游戏
- C#中分布式事务的超时处理问题
- C#中的WebBrowser控件的使用
- C#正则表达式2
- C# UDP服务编程,数据处理流程设计(抛砖引玉)
- C#时间戳与时间互转
- C# DataTable删除空行
- C#中yield return用法分析
- c#如何跨越线程调用窗体上的控件(转)
- 纯C#实现Hook功能
- C#全局键盘监听(Hook)的使用
- C# ListView用法
- Prevent a Process from Being Created Again in VC or C#
- C# webservices 之发布调用【3】
- C# 自定义FileUpload控件
- c# ToString() 格式化字符串 string.Format