0
我正在编写我的第一个ASP.NET MVC应用程序,对我来说存在一个大问题。我想制作一个代表表单的控件,但是当我尝试生成标签和文本框时,它会返回给我空白页面。Html.LabelFor和Html.TextBoxFor生成empy html代码
所以,这是我的模型文件(MyModel.cs):
namespace MyNamespace.Models
{
public class MyModel
{
[Required(ErrorMessage = "You have to fill this field")]
[DisplayName("Input name")]
public string Name{ get; set; }
}
}
这与我的控制MyFormControlView.ascx文件:
<%@ Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl<MyNamespace.Models.MyModel>"%>
<div>
<%
using (Html.BeginForm())
{
Html.LabelFor(m => m.Name);
Html.TextBoxFor(m => m.Name);
Html.ValidationMessageFor(m => m.Name);
}
%>
</div>
这是我的Index.aspx我提供控件的文件:
<%@ Page Language="C#" MasterPageFile="~/Views/Shared/Main.Master" Inherits="System.Web.Mvc.ViewPage<System.Collections.IEnumerable>" %>
<asp:Content runat="server" ID="MainContent" ContentPlaceHolderID="MainContent">
This is my control test!
<%Html.RenderPartial("MyFormControlView", new MyNamespace.Models.MyModel { Name = "MyTestName"}); %>
</asp:Content>
因此,当我运行我的a应用结果是孤独的标题:“这是我的控制测试!”并且生成的页面上没有标签或文本框。 如果我检查生成的页面的源代码,我可以看到我的块,但它的内部文本是空的。 请你能帮我吗?
你的代码不适用于我,它会返回一个错误“需要分号”。但我想这是因为我使用ASPxView引擎而不是Razor。我发现解决方案取决于你的代码:<%= Html.LabelFor(m => m.Name)%> - 这对我很有用。谢谢! – Ceridan
@Ceridan:你是对的;我曾以为你在使用剃刀。固定。 – SLaks