Date field autocompletion in the application

1

I have a form in my C # ASP.Net application where I need to fill in an end date, but I want to standardize so that the end date is always 5 days after the start date. How to do this? I believe that a Javascript would solve, but I have no experience with it.

<td>
    @Html.DropDownList("Nome", string.Empty)
</td>
<td>
    @Html.EditorFor(x => x.Date_Inicial)
</td>
<td>
    @Html.EditorFor(x => x.Data_Retorno)
</td>

or something in the controller like,

item.Data_Retorno = data_inicial.AddDays(5);

Note: I do not know how to do this, if I do in control what to change in View or using a Javascript as declared in the view. Data_Retorno must be filled in automatically 5 days after the start date as it is entered and should not be possible to edit it. How can I make it automatically visible? Type, the user types the start date (which is not necessarily the current date of the day), the return date automatically appears in the text box.

    
asked by anonymous 29.08.2014 / 20:57

2 answers

2

By means of several alternatives, an example is shown below by sending information from controller to its referent view .

Class Model

public class Professor
{
    public int Id { get; set; }
    public String Nome { get; set; }
    public DateTime DataInicial { get; set; }
    public DateTime DataFinal { get; set; }
}

ActionResult

public ActionResult Pessoas()
{
    DateTime Data = DateTime.Now;
    return View(new Professor() { Id = 0, Nome = "Nome 1", DataInicial = Data, DataFinal = Data.AddDays(5) });
}

Page

@model WebApi.Models.Professor
@{
    Layout = null;
}
<!DOCTYPE html>
<html>
<head>
    <meta name="viewport" content="width=device-width" />
    <title>Pessoas</title>
</head>
<body>
    @using (Html.BeginForm()) 
    {
        @Html.AntiForgeryToken()

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

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

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

            <div class="form-group">
                <div class="col-md-offset-2 col-md-10">
                    <input type="submit" value="Create" class="btn btn-default" />
                </div>
            </div>
        </div>
    }
</body>
</html>
    
29.08.2014 / 21:59
2

If Data_Retorno should always be 5 days greater than Date_Inicial , compute this in the method executed in POST of your controller:

[HttpPost]
public void Index(string nome, DateTime Date_Inicial)
{
    (...)
    DateTime Data_Retorno = Date_Inicial.AddDays(5);
}

2014-08-29 17:59

To display in the view, do:

Model

public class MinhaModel
{
    (...)
    public string Nome { get; set; }
    public DateTime Date_Inicial { get; set; }
    public DateTime Data_Retorno { get; set; }
}

Controller

public ActionResult Index()
{
    var obj = new MinhaModel;
    (...) // O que quer que alimente a classe

    obj.Data_Retorno = obj.Date_Inicial.AddDays(5);

    return View(obj);
}

View

@model Model.MinhaModel

(...)

@Html.EditorFor(model => model.Nome)
@Html.EditorFor(model => model.Date_Inicial)
@Html.EditorFor(model => model.Data_Retorno)
    
29.08.2014 / 21:58