0

I have an web app and it has a controller without a model. I'm am trying to get some information (in this case dateFrom) and I want to be passed in a method (of type ActionResult). The method it has the following signature GetInfo(DateTime dateFrom). Any ideas?

The code from razor is something like this:

<form method="post" [email protected]("GetInfo", "GetInfo", new  {dateFrom=getFromUser})>
    <input name="dateFrom" type="date"  />
    <input type="submit" />
</form>
1
  • the code from razor is something like this: <form method="post" [email protected]("GetInfo", "GetInfo", new { dateFrom=getFromUser})> <input name="dateFrom" type="date" /> <input type="submit" /> </form> Commented May 7, 2014 at 14:45

2 Answers 2

2

You can access dateFrom like this,

public ActionResult GetInfo()
{
    string sFromDate = Request.Form["dateFrom"];
    return View();
}

You can further convert sFromDate to DateTime object.

Sign up to request clarification or add additional context in comments.

Comments

1

set your code up like this instead:

In razor view:

@using (Html.BeginForm("GetInfo", "MyController"))
{
    <input name="dateFrom" type="date"  />
    <input type="submit" />
}

In MyController controller:

public class MyController : Controller
{
    public ActionResult GetInfo(string dateFrom)
    {
        var test = (DateTime)dateFrom;
    }
}

dateFrom should automatically be populated with the date supplied by the user. If they do not supply a format that can be converted by .Net, it will be null.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.