Amol
Amol

Reputation: 1461

Getting null value in user control mvc

I am trying to add user control for textbox having calender

My View

  @Html.Partial("~/Views/Shared/EditorTemplates/DateTime.ascx", "FromDate");

My User Control

<%@ Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl<String>" %>

<%= Html.TextBox(ViewData.ModelMetadata.PropertyName, new { @class="datepicker"} )%>

<script type="text/javascript">
$(document).ready(function () {
    $('.datepicker').datepicker({
        changeMonth: true,
        changeYear: true,
        dateFormat: 'mm-dd-yy',
        showButtonPanel: true,
        gotoCurrent: true
    });
});

My Controller

[HttpGet]
    public ActionResult SetTask()
    {
        SetTask model = new SetTask();

        var data = (from d in edc.ProjectTBs
                    select d).ToList();


        model.ProjectList = new SelectList(data, "ProjectID", "ProjectName").ToList();
        model.ProjectList.Insert(0, new SelectListItem { Text = "--Select--", Value = "0" });

       // ModelState.Clear();

        return View(model);
    }

Error Message

Value cannot be null or empty.
Parameter name: name

Please help me for getting value passed from view to user control.

I want to use multiple time on same page of this user control. If there is any other way to like this functionality also helpful.

Upvotes: 0

Views: 197

Answers (1)

Ashwini Verma
Ashwini Verma

Reputation: 7525

Instead of Partial View use EditorFor.

Call it like this: <%= Html.EditorFor(m => m.PropertyName) %>

Use this sample if you want in proper way.

<%@ Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl<DateTime?>" %>


 <%= Html.TextBox("", (Model.HasValue ? Model.Value.ToString("dd-MM-yyyy") : string.Empty), new { @class = "datepicker" }) %>


<script type="text/javascript">
    $(document).ready(function() {
        $('.datepicker').datepicker({
    changeMonth: true,
    changeYear: true,
    dateFormat: 'mm-dd-yy',
    showButtonPanel: true,
    gotoCurrent: true
});
    });
</script>

Upvotes: 2

Related Questions