Html.LabelFor and Html.TextBoxFor generate empy html code
Posted
by
Ceridan
on Stack Overflow
See other posts from Stack Overflow
or by Ceridan
Published on 2012-11-14T16:57:04Z
Indexed on
2012/11/14
16:59 UTC
Read the original article
Hit count: 497
I'm writing my first ASP.NET MVC application and there is one big problem for me. I want to make a control which will represent a form, but when I try to generate labels and textboxes it returns to me empty page.
So, this is my model file (MyModel.cs):
namespace MyNamespace.Models
{
public class MyModel
{
[Required(ErrorMessage = "You have to fill this field")]
[DisplayName("Input name")]
public string Name{ get; set; }
}
}
This is MyFormControlView.ascx file with my control:
<%@ 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>
And this is my Index.aspx file where I render the control:
<%@ 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>
So, when I run my application the result is lonely caption: "This is my control test!" and there are no label or textbox on the generated page. If I inspect the source code of the generated page I can see my block, but it's inner text is empty. Please, could you help me?
© Stack Overflow or respective owner