简体   繁体   中英

How to pass parameters from View to Controller without using Ajax in MVC4?Is it possible?

Hi is it possible to send parameters from view to controller without using ajax?

My View

  @Html.LabelFor(model => model.FromDate)
  @Html.TextBoxFor(model => model.FromDate, new { @class = "form-control", type = "text" })

  @Html.LabelFor(model => model.ToDate)
  @Html.TextBoxFor(model => model.ToDate, new { @class = "form-control", type = "text" })

 <input id="Button1" type="button" value="Ok" />

My controller

public ActionResult GetDates(string FromDate, string ToDate)
{
    DateTime fromdt = Convert.ToDateTime(FromDate);
    DateTime todt = Convert.ToDateTime(ToDate);
    SqlConnection con = new SqlConnection(@"Data Source=192.168.0.73\SQLEXPRESS,14330;Initial Catalog=WafeERP_NEW;User ID=sa;Password=wafewin;");
    DataTable dt = new DataTable();
    try
    {
        con.Open();
        SqlCommand cmd = new SqlCommand("Select * from View_VisitorsForm where  VisitingDate >='" + fromdt  +"'and VisitingDate <= '" + todt  +"'", con);
        SqlDataAdapter adp = new SqlDataAdapter(cmd);
        adp.Fill(dt);

    }
    catch (Exception ex)
    {

        throw;
    }
    ReportClass rc = new ReportClass();
    rc.FileName = Server.MapPath("/Reports/rpt_DailyCustomerVisitSummary.rpt");
    rc.Load();
    rc.SetDataSource(dt);
    Stream stream = rc.ExportToStream(CrystalDecisions.Shared.ExportFormatType.PortableDocFormat);
    return File(stream, "application/pdf");
}

Hi i have two fields in my view if i select the From Date and To Date and click the ok button it needs to pass these two dates to controller that is here

public ActionResult GetDates(string FromDate, string ToDate )

Is it possible? Using ajax it is possible but i don't want that. I want to pass these parameters to controller without using ajax. please any one tell me the solution.

Advance thanks..

Setting up a form you should be able to pass the parameters to your controller.

@Using(Html.BeginForm()){
  @Html.LabelFor(model => model.FromDate)
  @Html.TextBoxFor(model => model.FromDate, new { @class = "form-control", type = "text" })

  @Html.LabelFor(model => model.ToDate)
  @Html.TextBoxFor(model => model.ToDate, new { @class = "form-control", type = "text" })

 <input id="Button1" type="button" value="Ok" />
}

Also since it appears you are using a model you can pass the model back to the controller instead. Also making sure to set the [HttpPost] data annotation to signify that it is for posting data to.

[HttpPost]    
public ActionResult GetDates(YourModel yourModel)
{
    var from = yourModel.FromDate;
    var to = yourModel.ToDate;
}

You should just consider creating a <form> to wrap around your content and simply posting it to the server using a traditional form submission. This is generally the most common way that this type of behavior is handled.

So you would just need to wrap the contents in your View using either a bare bones <form> element that points to your specific URL (in this case it is resolved using the Url.Action() method :

<form action='@Url.Action("GetDates","YourController")'>
    @Html.LabelFor(model => model.FromDate)
    @Html.TextBoxFor(model => model.FromDate, new { @class = "form-control", type = "text" })
    @Html.LabelFor(model => model.ToDate)
    @Html.TextBoxFor(model => model.ToDate, new { @class = "form-control", type = "text" })
   <input id="Button1" type="submit" value="Ok" />
</form>

Since you are using MVC, you could also consider using the Html.BeginForm() helper as well, which will essentially do the same thing.

Then just ensure that your Controller Action is decorated with an [HttpPost] attribute so that it can actually accept POST requests :

[HttpPost]
public ActionResult GetDates(string FromDate, string ToDate)
{
    // Omitted for brevity
}

Additionally, since you are already binding your View to a model, you can actually change your parameter of your GetDates() method to expect an instance of your Model (which will be populated by .NET's model binding) :

[HttpPost]
public ActionResult GetDates(YourModel model)
{
    // Omitted for brevity (you can access your properties using
    // Model.FromDate and Model.ToDate respectively here
}

Use one of the FormExtensions.BeginForm methods. In the simplest scenario:

@using (Html.BeginForm("GetDates", "YourController"))
{
    @Html.LabelFor(model => model.FromDate)
    @Html.TextBoxFor(model => model.FromDate, new { @class = "form-control", type = "text" })

    @Html.LabelFor(model => model.ToDate)
    @Html.TextBoxFor(model => model.ToDate, new { @class = "form-control", type = "text" })

    <input type="submit" value="Ok" />
}

The BeginForm method renders a form that will be handled by a controller action method.

You can use this method in a using block. In that case, the method renders the closing </form> tag at the end of the using block.

You can pass whole model object instead of separatedProperties:

[HttpPost]
public ActionResult GetDates(YourModelClass model)
{
    ...
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM