値型用のASP.NET MVC 2エディターテンプレート、int
-
11-07-2019 - |
質問
値型(つまりint)用のMVC 2エディターテンプレートを作成したいのですが、プレビュー1ビットでこれを行った人はいますか?
多くの感謝
解決
私はまだプレビュー1を試していませんが、このchannel9ビデオであなたが求めていることをしました:
DisplayForとEditorForの両方を実行し、約2分で開始します。
-編集-
値型(つまり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>
次に、タイプごとにテンプレートを作成します。 Int32:
<%@ Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl" %>
Editor For My Int32: <%= Html.TextBox("abc", Model.ToString())%>
Views \ Shared \ EditorTemplates \ Int32.ascxに配置
他のヒント
ポストバックで値を送信すると、ニッククラークの回答は機能しますか?
MVC2プレビュー2では、Html.Textbox(&quot; abc&quot ;, Model.ToString())を呼び出します &quot; .abc&quot;でテキストボックスをレンダリングします名前に追加されます。
<input id="StartDate_abc" name="StartDate.abc" type="text" value="02 Feb 09" />
ポストバックしてUpdateModel()を試行すると問題が発生します。
DateTimeのエディターテンプレートを作成しましたが、次のように動作します:
/Views/Shared/EditorTemplates/DateTime.ascx:
<%@ Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl<DateTime>" %>
<%= Html.TextBox(String.Empty, Model.ToString("dd MMM yy")) %>
または、すべてのDateTimeにjQueryのDatePickerを使用する jQueryおよびjQueryUIへの参照を、マスターページまたはEditorForの呼び出しを含むビューに追加します。
/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 、Razor構文を使用:
@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
とテンプレートの関係についても説明しています。
ブラッドウィルソンのブログで最高の例と説明をご覧ください。 パート3 シリーズの中で、特に値の型(String、decimal、Int32)について説明しています。
お楽しみください