【问题标题】:ASP.NET MVC Using JQuery DatePicker with model bindingASP.NET MVC 使用带有模型绑定的 JQuery DatePicker
【发布时间】:2010-11-10 23:07:58
【问题描述】:

我正在尝试将 DatePicker 选择的日期绑定到在我的视图中强类型化的模型。我试图绑定到的模型(NewEditDataTypeModel)内的属性是“codeTypeNewEditModel.EffectStartDate”。数据类型为 DateTime。

我的观点强类型如下:

<%@ Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl<TMS.MVC.BusinessSystemsSupport.Models.NewEditDataTypeModel>" %>

** 我的 DatePicker 设置如下:**

Effective Start Date: <input type="text" id="Model_codeTypeNewEditModel_EffectStartDate" name="Model_codeTypeNewEditModel_EffectStartDate"/>


<script type="text/javascript">
$(document).ready(function () {
$("#Model_codeTypeNewEditModel_EffectStartDate").datepicker({
  showOn: 'button',
  buttonImage: '/Content/images/calendar.gif',
  duration: 0 
});
});

我的操作方法如下:

    [HttpPost]
    public ActionResult DataTypeNewEdit(NewEditDataTypeModel newEditDataTypeModel)
    {  etc

newEditDataTypeModel 模型不会从 DatePicker 填充受影响的开始日期。有谁知道我做错了什么?我相信使用下划线是正确的做法。

【问题讨论】:

    标签: jquery asp.net-mvc binding


    【解决方案1】:

    您不需要将Model 作为输入名称的前缀。

    输入的名称必须使用点来表示属性。没有下划线

    所以如果你要替换:

    <input type="text" id="Model_codeTypeNewEditModel_EffectStartDate" name="Model_codeTypeNewEditModel_EffectStartDate"/>
    

    <input type="text" id="Model_codeTypeNewEditModel_EffectStartDate" name="newEditDataTypeModel.EffectStartDate"/>
    

    它应该适合你。

    这里newEditDataTypeModel应该等于action上的参数。

    但我只会利用强类型助手并使用Html.TextBoxFor

    【讨论】:

    • '首先,你说你的视图是强类型的,但是根据代码它不是。这就是我要发布的内容,但随后我单击了编辑并看到了这一点,因为在...ViewUserControl 由于&lt; 而被修剪掉之后,他没有使用代码格式化内容。无论如何,这是正确的答案。
    • 我更新了答案以反映这一点。最后还为您添加了另一个选项。介意接受答案吗?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-05-25
    • 2011-03-15
    • 1970-01-01
    相关资源
    最近更新 更多