Pregunta

@{
    var dateHeader = DateTime.now;
}    


<form action="" medthod="post" name="dateNav">
    <input type="submit" value="<"/>
    <input type="text" readonly="readonly" value="@dateHeader.ToLongDateString()"/>
    <input type="submit" value=">"/>
</form>

I have this simple form. And what I want to do is to increase or decrease the date value in the text input by one day (DateTime.AddDays(1) or DateTime.AddDays(-1)).

In addition, I have some other forms in the code, so I only want to grab this one when posted. And I don't want to depend on client side solutions like jquery. Could I please have a pointer in the right direction?

¿Fue útil?

Solución

If you don't want to use any client side script, you could try this solution:

@{
   var dateHeader = DateTime.Now;
    if (IsPost) {
        dateHeader = DateTime.Parse(Request["curDate"]);
        if (Request["submit"] == ">")
        {
            dateHeader = dateHeader.AddDays(1);
        } else if (Request["submit"] == "<") {
            dateHeader = dateHeader.AddDays(-1);
        }
    }
}

<form action="" method="post" name="dateNav">
    <input type="submit" name="submit" value="<"/>
    <input type="text" name="curDate" readonly="readonly" 
        value="@dateHeader.ToLongDateString()"/>
    <input type="submit" name="submit" value=">"/>
</form>
Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top