MVC razor datepicker - no nie dziala

0

Hej,

już nie mam pomysłu co jest nie tak. Próbuję dodać datepicker do widoku. W widoku korzystam z modelu w którym mam 2 pola typu DateTime. Następnie używam EditorFor.

Mam dodane jquery, jquery-ui. Mam dodany folder EditorTemplates oraz DisplayTemplate w ktorym mam widok datetime.cshtml i taki kod:

@model DateTime

@Html.TextBod("", Model.ToString("dd/MM/yyyy"))

Próbowałem używać tych przykładów:
http://stackoverflow.com/questions/10251153/how-to-get-a-datepicker-for-html-editorfor
http://stackoverflow.com/questions/4576209/asp-net-mvc-3-razor-adding-class-to-editorfor

i wiele wiele innych i nic, po prostu nie działa.
Czy mogę prosić aby mi ktoś to napisał krok po kroku co mam zrobić? :(

EDIT:
działa mi to na zwykłym input, ale nie działa mi na editorFor, może jakiś textboxfor ?

0

Ale co Ci nie działa? Pokaż kod swojego cshtml i js.

0

Model:

   public class WynajemVm
    {
        public DateTime Od { get; set; }
        public DateTime Do { get; set; }
        public string Uwagi { get; set; }
        public string ListaKodow { get; set; }
        public int MagazynId { get; set; }
}

Widok:

@model Admin.Models.Create.WynajemVm


<link type="text/css" href="http://code.jquery.com/ui/1.11.2/themes/smoothness/jquery-ui.css"/>
<script src="http://code.jquery.com/ui/1.11.2/jquery-ui.js"></script>

@using (Html.BeginForm()) 
{
    @Html.AntiForgeryToken()
  
    <div class="form-horizontal">
        <h4>Nowy Wynajem</h4>
        <hr />
        @Html.ValidationSummary(true, "", new { @class = "text-danger" })
        <div class="form-group">
            @Html.LabelFor(model => model.Od, htmlAttributes: new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                <input type="text" id="dateOd" name="dateOd"/>
                @Html.EditorFor(model => model.Od, "",new {@class = "text-danger"})
                @Html.ValidationMessageFor(model => model.Od, "", new { @class = "text-danger" })
            </div>
        </div>

        <div class="form-group">
            @Html.LabelFor(model => model.Do, htmlAttributes: new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                @Html.EditorFor(model => model.Do, new { @class = "datetimepicker" })
                @Html.ValidationMessageFor(model => model.Do, "", new { @class = "text-danger" })
            </div>
        </div>

        <div class="form-group">
            @Html.LabelFor(model => model.Uwagi, htmlAttributes: new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                @Html.EditorFor(model => model.Uwagi, new { htmlAttributes = new { @class = "form-control" } })
                @Html.ValidationMessageFor(model => model.Uwagi, "", new { @class = "text-danger" })
            </div>
        </div>

        <div class="form-group">
            @Html.Label("Oddzielając przecinkiem, podaj listę kodów", htmlAttributes: new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                @Html.EditorFor(model => model.ListaKodow, new { htmlAttributes = new { @class = "form-control" } })
                @Html.ValidationMessageFor(model => model.ListaKodow, "", new { @class = "text-danger" })
            </div>
        </div>
        
            <div class="form-group">
            @Html.Label("Wybierz magazyn", htmlAttributes: new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                @Html.DropDownListFor(model => model.MagazynId, (IEnumerable<SelectListItem>)ViewBag.magazyny)
                @Html.ValidationMessageFor(model => model.ListaKodow, "", new { @class = "text-danger" })
            </div>
        </div>

        <div class="form-group">
            <div class="col-md-offset-2 col-md-10">
                <input type="submit" value="Dodaj" class="btn btn-default" />
            </div>
        </div>
    </div>
}

<div>
    @Html.ActionLink("Powrót", "Index", null, new { @class = "btn btn-default" })
</div>



<script type="text/javascript">

    $(function() {
        $('#dateOd').datepicker();
    });
</script>

Ten kawałek:

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

dodałem żeby zobaczyć czy na zwykłym input to zadziała - i działa. Natomiast nie działa mi na EditorFor, próbowałem z tym template, ale nie wyszło i usunąłem ten folder

1

ja u siebie mam coś takiego w widoku:

<div class="form-group">
        @Html.LabelFor(m => m.Date)
        @Html.TextBoxFor(m => m.Date, new { @class = "form-control", @id = "date-eaten", @Value =          DateTime.Today.ToShortDateString() })
        @Html.ValidationMessageFor(m => m.Date)
</div>

i jquery

$(document).ready(function () {
    $("#date-eaten").datepicker({
    })
})
0

Stary dzięki!!!
Działa!!! :D

0

Kurde jeszcze jedna rzecz, dodalem DateTimePicker a nie DatePicker, no i teraz tak mi sie skopal widok, jak to naprawic?

1 użytkowników online, w tym zalogowanych: 0, gości: 1