无法在asp.net mvc页面中看到文本框
本文关键字:文本 asp net mvc | 更新日期: 2023-09-27 18:24:33
我对asp.net mvc完全陌生,这是我第一个需要在一个视图中显示一个文本框的示例项目。当用户在该文本框中输入值时,我需要在另一个视图的标签中显示该值。为此我做了这样的事。。
这是我的控制器类
public class TextBoxController : Controller
{
//
// GET: /TextBox/
public ActionResult Index()
{
return View();
}
}
这是我的型号
namespace MvcTestApplication.Models
{
public class TextboxModel
{
[Required]
[Display(Name= "Textbox1")]
public string EnteredValue { get; set; }
}
}
这是我的观点
@model MvcTestApplication.Models.TextboxModel
@{
ViewBag.Title = "TextboxView";
}
<h2>TextboxView</h2>
@using (Html.BeginForm())
{
<div>
<fieldset>
<legend>Enter Textbox Value</legend>
<div class ="editor-label">
@Html.LabelFor(m => m.EnteredValue)
</div>
<div class="editor-field">
@Html.TextBoxFor(m=>m.EnteredValue)
</div>
<p>
<input type="submit" value="Submit Value" />
</p>
</fieldset>
</div>
}
我看不到页面上的任何文本框和任何按钮,我收到了类似的错误
HTTP:404:找不到资源
我正在使用visual studio 2012和mvc4.
请对这件事提出意见好吗。。非常感谢。。
重新写入
简单地说,要访问ASP.NET MVC上的页面,您应该将URL指向其控制器名称。在这种情况下,TextBox
:
localhost:2234/TextBox/TextBox
此外,您忘记为此新视图添加ActionResult
。当您加载页面时,它将通过空的Index
页面。
最后的代码应该是这样的:
控制器
public class TextBoxController : Controller
{
public ActionResult Index()
{
return View();
}
public ActionResult TextBox(MvcApplication1.Models.TextBoxModel model)
{
return View(model);
}
}
型号
public class TextBoxModel
{
[Required]
[Display(Name = "Textbox1")]
public string EnteredValue { get; set; }
}
剃刀视图(索引)
@{
ViewBag.Title = "Index";
}
<h2>Index</h2>
剃刀视图(文本框)
@model MvcApplication1.Models.TextBoxModel
@{
ViewBag.Title = "TextBox";
}
<h2>TextBox</h2>
@using (Html.BeginForm())
{
<div>
<fieldset>
<legend>Enter Textbox Value</legend>
<div class ="editor-label">
@Html.LabelFor(m => m.EnteredValue)
</div>
<div class="editor-field">
@Html.TextBoxFor(m=>m.EnteredValue)
</div>
<p>
<input type="submit" value="Submit Value" />
</p>
</fieldset>
</div>
}
确保您已经通过路由配置注册了URL。
点击此处查找有关asp.net路由的更多信息
更新:
请确保视图的文件名为Index.cshtml
,因为控制器没有指定任何返回视图名称。