2016-12-20 1 views
0

mvcの問題で作業中で、datetimepickerデータをコントローラアクションにバインドしようとして問題が発生しました。私は、ユーザーが何らかのアクションを行うときに、部分的なビューのコレクションに部分的な部分を追加するビューを持っています。Boostrap DatetimepickerでHtml.hiddenがコントローラアクション内の値をバインドしていない

私のモデルは、ユーザーが、私がいるICollectionのrestringidasに新しい項目を追加し、いくつかのエーションを行うと、私は

@using (Html.BeginForm("CreateOC", "OC", FormMethod.Post, new { id = "form" })) 

<table id="personasRestringidas" class="table table-striped table-bordered table-hover"> 
           <thead> 
            <tr> 
             <th>id</th> 
             <th>Motivo</th> 
             <th>Fecha de acceso</th> 
             <th>Eliminar</th> 
            </tr> 
           </thead> 
           <tbody id="person"> 
            @foreach (PersonaMin item in Model.restringidas) 
            { 

              @Html.Partial("~/Views/OC/AddOCRestriccion.cshtml", item) 
            } 
           </tbody> 
          </table> 

を持って私の見解では

public class CreateOCVM 
{ 

    private string errors { get; set; } 

    public operaciones_confidenciales oc { get; set; } 

    [Display(Name = "Restringidas")] 
    public ICollection<PersonaMin> restringidas { get; set; } 


public class PersonaMin 
{ 
    public int Id { get; set; } 
    public string motivo { get; set; } 
    [DataType(DataType.Date)] 
    public DateTime fecha_acceso; 

のように見えます。次のように制御方法AJAX呼び出しを行うことで:

function addRestriccion(idPersona) { 
if (jQuery.inArray(idPersona, dataset) != 0) { 
    $.get('/OC/AddOCRestriccion', { id: idPersona}, function (template) { 
     dataset.push(idPersona); 
     $("#person").append(template); 
     $('#datetimepicker' + idPersona).datetimepicker(); 
     $('#datetimepicker' + idPersona).datetimepicker({ format: "DD/MM/YYYY hh:mm" }); 
     $("#datetimepicker" + idPersona).on("change.dp", function (e) { 
      $('#fechaAcceso' + idPersona).val($("#datetimepicker" + idPersona).datepicker('getFormattedDate')) 
     }); 
    }); 
} 

この制御方法ANはメインビューに追加される部分的なビューを返し呼び出します。

 [HttpGet] 
    public ActionResult AddOCRestriccion(Int32 id) 
    { 
     PersonaMin item = new PersonaMin(); 
     item.Id = id; 
     persona p = PersonaCollection.getPersonasById(id); 
     return PartialView("AddOCRestriccion", item); 
    } 

編集: このコントローラは、主なものに追加する部分図を返します。私は、フォームを送信すると、それはコントローラのアクションによって処理され

<tr> 
@using (Html.BeginCollectionItem("restringidas")) 
{ 
    <td> 
     @Html.HiddenFor(model => model.Id) 
     @Html.DisplayFor(model => model.Id, new { @placeholder = "id", @id = "ID" }) 
    </td> 
    <td> 
     @Html.TextAreaFor(model => model.motivo, new { @placeholder = "motivo", @id = "motivo", @class = "form-control" }) 
    </td> 
    <td> 
     <div class='input-group date' id="@("datetimepicker" + Model.Id)"> 
      <input type='text' class="form-control" /> 
      <span class="input-group-addon"> 
       <span class="glyphicon glyphicon-calendar"></span> 
      </span> 
      @Html.HiddenFor(model => model.fecha_acceso, new { @id = "fechaAcceso" + Model.Id }) 
     </div> 
    </td> 
    <td class="text-center"> 
     <span class="input-group-btn"> 
      <button type="button" class="btn btn-primary" onclick="deleteAnother($(this).parent().parent())"> 
       <i class="fa fa-minus"></i> 
      </button> 
     </span> 
    </td> 

} 

 [HttpPost] 
    public ActionResult CreateOC(CreateOCVM ocvm) 
    { ... do something .... 

問題は、私は私のモデルに値を確認する場合、すべての値がOKではないということです1つは日時用です(fecha_acceso)

私の見解では、私のdatetimeプロパティの隠しフィールドには値があります。

enter image description here

しかし、私は、コントローラのメソッドでそれをチェック値変更。

enter image description here

私はdiferent aproachesと試みたが、私はいつも私のコントローラメソッドに誤ったデータを取得します。誰かが何が起きているのか知っていますか?それとも誰かに同じことをするのに役立つ別の方法がありますか?

ありがとうございました。

答えて

0

デフォルトでは、コントローラは日付形式をdd-mm-yyyyとして許可しません。私Html.BeginCollectionItemヘルパーは、文字列への日時からの私のviewmodelのDateTimeのようにyの変更・ド・タイプの問題を持っているようにそれ

<configuration> 
    <system.web> 
     <globalization culture="en-GB" /> 
    </system.web> 
</configuration> 
+0

これを試してもうまくいきませんでした。また、コントローラに渡す前にdatetimeをフォーマットしようとしましたが、同じ振る舞いをしていました。 – Jose

+0

私もこれを試して、値としてnullを得ました – Jose

+0

もう一度やってスクリーンショットを送る必要がありますか? – Jose

0

が見える行うには、あなたのweb.configファイル内の行の下に追加 。私はそれが回避策であり、正しい解決策ではないが、時間の欠如が私にそれをさせることを強いることを知っている。

関連する問題