Automatic date field fill in application

Asked

Viewed 1,465 times

1

I have a form in my C# ASP.Net application where I need to fill in an initial and final date, but I want to standardize so that the final date is always 5 days after the initial date. How to do this? I believe 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);

Obs: I don’t know how to marry this, if I do in control what to change in View or using Javascript as decloded in the view. Data_Retorno should be automatically filled in 5 days after the initial date as soon as it is typed and it should not be possible to edit it.How do I make it automatically visible? Type, the user enters the initial date (which is not necessarily the current date of the day), automatically the return date appears in the text box.

2 answers

2

If Data_Retorno must at all times be 5 days longer than Date_Inicial, calculate 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; }
}

Controler

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)
  • Okay @Tiagocésaroliveira and in the view how do I place it? I would like it to be visible in the form that date

  • @Pfvictor edited the answer.

  • Thanks for the answers. I can through them automatically record in the bank the return date as 5 days after the current date. And how do I get the return date 5 days after the date the user enters? (not necessarily the date of the day) Also the return date does not appear in the textbox, only saved in the bank. How do I make it visible automatically? Type, user type start date, automatically return date appears in text box? @Tiagocésaroliveira

  • @Pfvictor, please edit your question with the full requirement.

  • Actually @Tiagocésaroliveira this is already in the question. I did not say that the initial date is current, but the date that the user type. But I improved it there. Thank you.

2

Through the various alternatives below follows an example by sending the information of the controller for your reference 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>

Browser other questions tagged

You are not signed in. Login or sign up in order to post.