Passing Parameters from View to Controller

Passing parameters from view to controller

You can add information to the params hash right through the link_to. I'm not sure exactly what you are trying to do but I did something like this recently to add the type of email I wanted when I link to the new email

<%= link_to 'Send Thanks', new_invoice_email_path(@invoice, :type => "thanks") %>

Now my params looks like:

{"type"=>"thanks", "action"=>"new", "controller"=>"emails", "invoice_id"=>"17"}

I can access the type via the params

email_type = params[:type]

Instead of a string, if you pass in the instance variable @rela you will get the object_id in the params hash.

Per the comment below, I'm adding my routes to show why the path new_invoice_email_path works:

resources :invoices do
resources :emails
end

ASP.Net MVC How to pass data from view to controller

You can do it with ViewModels like how you passed data from your controller to view.

Assume you have a viewmodel like this

public class ReportViewModel
{
public string Name { set;get;}
}

and in your GET Action,

public ActionResult Report()
{
return View(new ReportViewModel());
}

and your view must be strongly typed to ReportViewModel

@model ReportViewModel
@using(Html.BeginForm())
{
Report NAme : @Html.TextBoxFor(s=>s.Name)
<input type="submit" value="Generate report" />
}

and in your HttpPost action method in your controller

[HttpPost]
public ActionResult Report(ReportViewModel model)
{
//check for model.Name property value now
//to do : Return something
}

OR Simply, you can do this without the POCO classes (Viewmodels)

@using(Html.BeginForm())
{
<input type="text" name="reportName" />
<input type="submit" />
}

and in your HttpPost action, use a parameter with same name as the textbox name.

[HttpPost]
public ActionResult Report(string reportName)
{
//check for reportName parameter value now
//to do : Return something
}

EDIT : As per the comment

If you want to post to another controller, you may use this overload of the BeginForm method.

@using(Html.BeginForm("Report","SomeOtherControllerName"))
{
<input type="text" name="reportName" />
<input type="submit" />
}

Passing data from action method to view ?

You can use the same view model, simply set the property values in your GET action method

public ActionResult Report()
{
var vm = new ReportViewModel();
vm.Name="SuperManReport";
return View(vm);
}

and in your view

@model ReportViewModel
<h2>@Model.Name</h2>
<p>Can have input field with value set in action method</p>
@using(Html.BeginForm())
{
@Html.TextBoxFor(s=>s.Name)
<input type="submit" />
}

How to pass parameters from view to controller in Yii2

view:

<?= Html::a(Yii::t('app', 'Search'), ['search','stop'=>$stop,'stops'=>$stops], ['class' => 'btn btn-success']) ?>

controller:

public function actionSearch($stop,$stops)
{
return $this->render('search', ['stop' => $stop, 'stops' => $stops]);
}

pass parameter from View to Controller in asp mvc

Please use the following in your cshtml page

  @foreach (var item in Model)
{
@Html.ActionLink(item.PriceDetails, "GetGift", new { priceID = item.priceID }, new { @class = "lnkGetGift" })
}

<script type="text/javascript" src="~/Scripts/jquery-1.10.2.js"></script>
<script type="text/javascript">
$(document).ready(function () {
$("a.lnkGetGift").on("click", function (event) {
event.preventDefault();
$.get($(this).attr("href"), function (isEligible) {
if (isEligible) {
alert('eligible messsage');
}
else
{
alert('not eligible messsage');
}
})
});
});
</script>

and in controller

     [HttpGet]
public JsonResult GetGift(int priceID)
{
List<Price_T> priceimg = (from x in dbpoints.Price_T
select x).Take(3).ToList(); ;
ViewBag.PriceTotal = priceimg;
var allpoint = singletotal.AsEnumerable().Sum(a => a.Points);
var price = from x in dbpoints.Price_T
where x.PriceId == id
select x.PricePoints;
int pricepoint = price.FirstOrDefault();
if (allpoint < pricepoint)
{
return Json(false, JsonRequestBehavior.AllowGet);
}
else
{
return Json(true, JsonRequestBehavior.AllowGet);
}
}

Please change your parameters according to the method param and price entity
Hope this helps.

Laravel 5.3 passing parameter from view to controller

You need to capture segments of the URI within your route.

Route::get('single/{id}', [
"uses" => 'ProductsController@single',
"as" => 'single'
]);

How to pass value from view to controller

there is another way.

[HttpPost]
public ActionResult Index() {

string name = Request["name"];
}

Pass a variable from a view to a controller action method and then to another method in the same ASP.NET MVC controller

You can use ViewData or TempData to persist your variables from one Controller action to another:

public JsonResult GetTokenFromView(string Token)
{
ViewData["PassedToken"] = Token;
return Json(Token);

// I have tried redirectToAction as well but the value becomes null.
}

And access it in your Thankyou method like this:

public ActionResult Thankyou(string slug, string Token, string email)
{
slug = "thank-you";
Console.Write(Token);
//Token = ViewBag.PassedToken;
//Get your ViewData variables here
if (ViewData["PassedToken"] != null)
{
Token=ViewData["PassedToken"];
}
var savedToken = Token;
Console.Write(savedToken);

var url = "https://application.ecomapi.com/api/purchases/" + savedToken;
var httpRequest = (HttpWebRequest)WebRequest.Create(url);

return View();
}


Related Topics



Leave a reply



Submit