Reputation: 2604
I've been facing this problem for a while now. I cannot find any article that could help solve this issue. The following are screenshots of the code and the output:
Web Interface
Code Snippet
How can I pass the value of this input:
<input type="text" id="ProjectName" name="ProjectName" class="form-control" />
to one of the parameters of the razor call:
Report.GenerateReport("project name here", "from date here","to date here");
Angular solutions are also welcome. Thanks!
Upvotes: 1
Views: 4320
Reputation: 14165
The standard way of doing this in Razor is to use the MVC HtmlHelper class.
@using(Html.BeginForm("GenerateReport", "Report", new { projectname = Model.ProjectName, fromMonth = Model.FromMonth , toMonth = Model.ToMonth }, FormMethod.Post))
{
//put your Input Fields here
}
You might not have a model. In this case, you can still use the above code by removing the parameters:
@using(Html.BeginForm("GenerateReport", "Report", FormMethod.Post))
{
//put your Input Fields here
}
Create a Controller called Report
and a method called GenerateReport
.
//Place this code in ReportController.cs
public ActionResult GenerateReport(string ProjectName, string FromDate, string ToDate)
{
//Add your code here
}
That will do the job.
Upvotes: 1
Reputation: 1280
If you are using RazorWebPages then you just need to add method attribute to your form tage like
<form method="post" class="form-horizontal">
then your following code will work properly
@{
Report.GenerateReport(Request["ProjectName"], "from date here","to date here");
}
but there will be need to convert your FromMonth and FromYear field to DateTime and check the project name is exist other wise Report.GenerateReport function throw Exception, so for that you can write that as
@{
var porjectName = Request["ProjectName"];
if (!string.IsNullOrEmpty(porjectName ))
{
var fromDate = DateTime.Parse(string.Format("{0}-{1}-{2}", Request["FromYear"], Request["FromMonth"], 1));
var toDate = DateTime.Parse(string.Format("{0}-{1}-{2}", Request["ToYear"], Request["ToMonth"], 1));
Report.GenerateReport(porjectName, fromDate, toDate);
}
}
You can find more information about asp.net razor webpage @ http://www.asp.net/web-pages/overview/getting-started/introducing-aspnet-web-pages-2/form-basics
Or if you are using Asp.net Mvc then you will need to write Get/Post Actions. In the Get Action your initial view will be rendered and by Submit button you will hit the Post Action where the best way will be load the values in a wrapper class called Model/ViewModel and then pass that model to your Report.GenerateReport method.
Controller Class:
public class ReportGeneratorController : Controller
{
[HttpGet]
public ActionResult ProjectReport()
{
return View();
}
[HttpPost]
public ActionResult ProjectReport(ReportParamModel model)
{
return View(model);
}
}
Your Model
public class ReportParamViewModel
{
public string ProjectName {get;set;}
public int FromMonth {get;set;}
public int FromYear {get;set;}
public int ToMonth {get;set;}
public int ToYear {get;set;}
public DateTime GetFromDate()
{
return new DateTime(FromYear, FromMonth, 1);
}
public DateTime GetToDate()
{
return new DateTime(ToYear, ToMonth, 1);
}
}
Your view or cshtml File
@Model ReportParamViewModel
<!-- your current html -->
@{
Report.GenerateReport(model.ProjectName, model.GetFromDate(),model.GetToDate());
}
You can find more information about Asp.net Mvc @ http://www.asp.net/mvc/overview/getting-started
Upvotes: 4
Reputation: 1233
here you go:
Report.GenerateReport(Request["ProjectName"], "from date here","to date here");
Request["ProjectName"] should get your input value.
There's also this problem in your code:
<form class="form-horizontal">
You need to add an "action" attribute to the form. For example:
<form action="ControllerName/ActionName" class="form-horizontal">
Upvotes: 1