1

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

enter image description here

Code Snippet

enter image description here

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!

5
  • 1
    what piece of code is actually triggering that function call? It appears that you are creating a form (client side) and then immediately executing the function (server side), but expecting to pass it values from the form, which couldn't possibly be filled in yet. Commented Nov 21, 2014 at 4:32
  • why angularjs tag, i cant see ng-controller/ng-app/ng-model anything Commented Nov 21, 2014 at 5:06
  • Could you post the code in controller and view name too? Commented Nov 21, 2014 at 6:15
  • 1
    Can you show a controller action definition for your code (the one being called on POST request from Generate Report button)? For me it seems like it's one of: - no post request of the form happens - post request does not actually submit the form values - parameters are not bound correctly in the controller action Commented Nov 21, 2014 at 16:21
  • Does your page have a controller? Commented Nov 27, 2014 at 23:47

3 Answers 3

4
+75

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

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

Comments

1

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">

Comments

1

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.

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.