문제

값 유형 IE int에 대한 MVC 2 편집기 템플릿을 만들고 싶습니다. 미리보기 1 비트로 이것을 한 사람이 있습니까?

많은 감사합니다

도움이 되었습니까?

해결책

나는 아직 미리보기 1을 시도하지 않았지만이 Channel9 비디오에서 당신이 요구하는 일을했습니다.

http://channel9.msdn.com/posts/glucose/hanselminutes-on-9-aspnet-mvc-preview-1-with-phil-haack-and-virtual-scott/

그들은 DisplayFor와 EditorFor를 모두 수행하고 약 2 분 동안 시작합니다.

--편집하다--

가치 유형의 경우 int int와 같은 방식으로 작동하도록 할 수있었습니다.

내 견해로 전달할 모델을 만듭니다.

public class HomeController : Controller
{
    public ActionResult Index()
    {
        HomeModel model = new HomeModel();
        model.message = "Welcome to ASP.NET MVC!";
        model.number = 526562262;
        model.Date = DateTime.Now;

        return View(model);
    }
}

public class HomeModel
{
    public string message { get; set; }

    public int number { get; set; }

    public DateTime Date { get; set; }
}

새로운 템플릿 로직을 사용하여 모델에 대한 링크보기 :

<%@ Page Language="C#" Inherits="System.Web.Mvc.ViewPage<HomeModel>" %>

<asp:Content ID="indexContent" ContentPlaceHolderID="MainContent" runat="server">
<p>
    <% Html.EditorFor(c => c.message); %>
</p>
<p>
    <% Html.EditorFor(c => c.number); %>
</p>
<p>
    <% Html.EditorFor(c => c.Date); %>
</p>

그런 다음 각 유형에 대한 템플릿을 만듭니다. eg int32 :

<%@ Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl" %>
Editor For My Int32: <%= Html.TextBox("abc", Model.ToString())%>

나는 이것을보기 shared editortemplates int32.ascx에 넣었다.

다른 팁

Postback에서 값을 제출할 때 Nick Clarke의 답변이 작동합니까?

MVC2 Preview 2에서 html.textbox ( "abc", model.toString ())을 호출하면 ".abc"가 이름에 추가 된 텍스트 상자를 렌더링합니다.

<input id="StartDate_abc" name="StartDate.abc" type="text" value="02 Feb 09" />

포스트 백일 때 문제가 발생하고 업데이트를 시도 할 때 ().

DateTime에 대한 편집기 템플릿을 사용했습니다. 다음은 다음과 같습니다.

/views/shared/editortemplates/datetime.ascx :

<%@ Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl<DateTime>" %>
<%= Html.TextBox(String.Empty, Model.ToString("dd MMM yy")) %>

또는 모든 datetimes에 jQuery의 datePicker를 사용하려면 jQuery 및 jQueryUi에 대한 참조를 마스터 페이지 또는 편집기 호출을 포함하는 뷰에 추가하십시오.

/views/shared/editortemplates/datetime.ascx :

<%@ Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl<DateTime>" %>
<%= Html.TextBox("", Model.ToString("dd MMM yy")) %>
<script type="text/javascript">
    $("#<%= ViewData.ModelMetadata.PropertyName %>").datepicker({ dateFormat: 'dd M y' });
</script>

업데이트 : ASP.NET MVC3, 면도기 구문 사용 :

@model System.DateTime
@Html.TextBox("",  Model.ToString("dd MMM yy"))
<script type="text/javascript">
    $("#@ViewData.ModelMetadata.PropertyName").datepicker({ dateFormat: 'dd M y' });
</script>

그리고 당신의 견해에 필요한 모든 것을 사용하는 것은 다음과 같습니다.

@Html.EditorFor(model => model.DueDate)

-매트

나는 썼다 블로그 게시물 MVC 2에서 재사용 가능한 템플릿을 만들어이 작업을 수행하는 방법에 대해.

내 게시물은 또한 간의 관계를 설명합니다 TemplateInfo 그리고 템플릿.

브래드 윌슨을 찾았습니다 블로그 최고의 예와 설명을 갖습니다. PART-3 이 시리즈 중 값 유형 (문자열, 소수점, int32)에 대해 구체적으로 이야기합니다.

즐기다!

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top