0

In asp.net mvc 5 there is a values on page, that user is entered or somehow it calculated by javascript. I need to put it in Html.BeginForm in section TO and FROM. The code sample is below:

MyPage.cshtml

<input id="datepickerFrom" value="31/12/2017" title="datepicker" />
<input id="datepickerTo" value="30/06/2018" title="datepicker" />
@using (Html.BeginForm("ExportToExcel", "DashBoard", new inputDates { from = "", to = "" }, FormMethod.Post))
{
     <input type="submit" value="Export to Excel" class="button" />
}

Controller.ActionName

public ActionResult ActionName(inputDates dates)
{
     //some actions 
       return new HttpStatusCodeResult(555, "Everything is OK");
}

Class inputDates

public class inputDates
{
     public DateTime? from { get; set; }
     public DateTime? to { get; set; }
}

I found a lot of answers that are using Model, but my values a are not from model. I don't know how to take values from html element or javascript values.

Thank you for help.

4
  • 2
    For a start, your inputs need to be inside the form. Then they need name attributes to match the model properties (i.e. name="from" etc). And your new inputDates { from = "", to = "" } in the BeginForm() make no sense (delete that) Commented Aug 16, 2018 at 9:46
  • Your BeginForm overload doesn't makes sense (see here). Use viewmodel properties and other helpers to pass those required elements. Commented Aug 16, 2018 at 9:50
  • Hi below link can help you How to send javascript var in Html beginform Commented Aug 16, 2018 at 9:56
  • @StephenMuecke is right, zalt you have to put "from" and "to" into form tag and set your html input controls name attribute same as your model property it bind with it. Commented Aug 16, 2018 at 10:07

1 Answer 1

0
   @model inputDates //do proper import of class here.

   @using (Html.BeginForm("ExportToExcel", "DashBoard", FormMethod.Post))
    {
         @Html.TextBoxFor(m => m.from)
         @Html.TextBoxFor(m => m.to)
         <input type="submit" value="Export to Excel" class="button" />
    }

This should work, just see if MVC automatically converts text to datetime or not. I have not used this in long time so my memory is hazy on this one. Also, read about Model Binding in MVC.

Some Links https://www.c-sharpcorner.com/UploadFile/4b0136/introduction-to-model-binding-in-Asp-Net-mvc/

https://odetocode.com/blogs/scott/archive/2009/04/27/6-tips-for-asp-net-mvc-model-binding.aspx

https://www.codeproject.com/Articles/710776/Introduction-to-ASP-NET-MVC-Model-Binding-An-Absol

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

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.