Input parameter of action is null when using asp-route-{value} - asp.net-mvc

I want to download a file that its name has been saved in my database. I use the following code to download the file:
public async Task<IActionResult> DownloadPm(string fileName)
{
.
.
.
}
And use the following code in view:
<form method="get">
<button type="submit" class="btn btn-success btn-table" asp-controller="Page" asp-
action="DownloadPm" asp-route-fileName="#item.mainFileName">ِDownload</button>
</form>
The problem is that the input parameter in DownloadPm action is null.

You are using tag helper with a button which actually does nothing but triggers the html form. To keep your current code you can have a hidden field in the form which will post the value to the action. Sample code:
<form method="get">
<input name="fileName" type="hidden" value="#item.mainFileName">
<button type="submit" class="btn btn-success btn-table" asp-controller="Page"
asp-action="DownloadPm">ِDownload</button>
</form>
Better approach: Since you are using Http GET and there is no other control in the form then why bother with Form? Instead go with an anchor element and style it like a button. Like the followings:
<a class="btn btn-success btn-table" href="/Page/DownloadPm?fileName="#item.mainFileName">Download</a>

Related

asp-action and asp-controller executes post method after reedirects

I use from asp-action in order to logout action as:
<form asp-action="Logoff" asp-controller="Account">
<button type="submit" class="btn-link dropdown-item">
<i class="mdi mdi-logout font-size-16 align-middle me-1"></i>
Logout
</button>
</form>
So I want to use the same to redirect to a new view, that view has a inside it and the controller has a HttpPost, so when I click on it executes the post method, is there a way to avoid the execution of HttpPost method and still using asp-actions?
I change the button type to button, but it do anything when it's clicked
You can try to add method="get" into the form,so that when you click on it executes the get request:
<form method="get" asp-action="Logoff" asp-controller="Account">
<button type="submit" class="btn-link dropdown-item">
<i class="mdi mdi-logout font-size-16 align-middle me-1"></i>
Logout
</button>
</form>

How to access to the new page with form and target="_blank" through capybara

I am trying to scrape this page with capybara + poltergeist.
<form action="/agency/advertiser/login" method="POST" role="form" target="_blank">
<input type="hidden" name="csrfToken" value="token"/>
<input type="hidden" name="account_id" value="id">
<input type="submit" value="login" class="btn btn-warning">
</form>
I could access to the element below, and tried click.
<input type="submit" value="login" class="btn btn-warning">
However, I can't access to the new page that opens after the click. How can I do that?
You need to tell Capybara you want to work in the new window. You do that by getting a handle to newly opened window and then using within_window or switch_window.
new_window = page.window_opened_by do
# perform whatever action causes the new window to open
page.find('form').click_button
end
page.within_window(new_window) do
# perform actions in the new window
end
# context returned to the original window
...

I want to POST the content of a uib-timepicker angular-ui bootstrap tag

I would like to let the user pick a time using a uib-timepicker tag and POST the result to my server (with a submit button for example).
<form class="simple_form vertical_form"
data-type="json" id="my_form"
action="url_to_post"
accept-charset="utf8"
data-remote="true"
method="post">
<div class="field">
<uib-timepicker
name="my_time"
ng-model="event.my_model"
hour-step="1"
minute-step="15"
show-meridian="true">
</uib-timepicker>
</div>
<div>
<input type="submit" value="Validate" class="btn btn-success">
</div>
</form>
But when I check to content of the POST method it is empty (I don't get the "my_time" key as I would expect. I guess it is because a "uib-timepicker" does not exactly work as an input field. But I feel like there must be a way to POST the content of that tag.
Thanks for helping!
I solved this issue by adding hidden input elements with binding to the time picker:
<uib-timepicker ng-model="ServiceRestoredTime" name="ServiceRestoredTime" ng-change="changed()" hour-step="hstep" minute-step="mstep" show-meridian="false" show-spinners="false"></uib-timepicker>
<input type="hidden" name="ServiceRestoredTimeParam" value="{{ServiceRestoredTime | date:'HH:mm' }}">

Login MVC redirect to another page

I have a log-in page. When the user hits the log-in button the button will calla another action method. that action method will have two parameter. the user provided userid and password. then it do some validation and redirect to another action method according to the outcome. I am struggling with there. i guess this is pretty simple. i am new to MVC. Any help would be appriciated.
View
<div class="container-fluid">
<div class="starter-template">
<h1>Policy Assessment Tool</h1>
<p class="lead">Are You Following Right?</p>
<button type="button" class="btn btn-primary btn-lg" onclick="location.href='#Url.Action("Create","UserDatas")'">Register for a An Account</button>
<h3><strong>OR</strong></h3>
</div>
<div class="row">
<form class="form-signin" role="form" method="post">
<div class="col-lg-4"></div>
<div class="col-lg-4">
<h2 class="form-signin-heading">Please Sign-In to Continue</h2>
<div class="input-group">
<input type="text" name="input_domain_id" class="form-control text-center" placeholder="Domain ID" autofocus required>
<input type="password" name="input_domain_password" class="form-control text-center" placeholder="Domain Password" required>
<div class="col-lg-12">
<p><button class="btn btn-lg btn-primary btn-block" type="submit" onclick="location.href='#Url.Action("Verify_Login", "Ldap_Login_Verify")'">Login</button></p>
</div>
</div><!-- /input-group -->
</div><!-- /.col-lg-4 -->
<div class="col-lg-4"></div>
</form>
</div><!-- /.row -->
</div>
Controller
[HttpPost]
public ActionResult Verify_Login(string input_domain_id, string input_domain_password)
//Log-in Logic
return View("Success")
Instead of using form tag use Html.BeginForm HtmlHelper and pass Model from Controller to View for the best practice. Here is link for you to get Getting Started With MVC5.
Based on the mark up above you need not worry about it, When the form is posted the values will get bound to the parameters of the action method, As the parameter names are similar to the form field names.
The above concept is called model binding. The below two links should give you a good idea on the same.
http://www.codeproject.com/Articles/710776/Introduction-to-ASP-NET-MVC-Model-Binding-An-Absol
http://dotnetslackers.com/articles/aspnet/Understanding-ASP-NET-MVC-Model-Binding.aspx
I have tried to compile a simple example below.
Lets assume there is a view like below.
#model string
#{
ViewBag.Title = "Injection";
}
<h2>Injection</h2>
#using(Html.BeginForm())
{
<div id="formDetails">
#Html.TextBox("UserName")
</div>
<div>
<input type="submit" id="btnTest" value="Click Me" />
</div>
}
The Action method for the same would be like
[HttpPost]
public ActionResult Injection(string UserName)
{
return View("Injection");
}
So when i click on the submit button, The form is posted , Since the ViewName is injection, and there is a corresponding action method called injection it will automatically hit that action method. Since the parameter name of the method and field is the same, What ever value is there in the username textbox will get automatically bound to the method parameter.

how to call method in button onclick event in MVC 4?

I am developing a MVC application.
In Edit View, I want to have save/submit button and cancel button.
By default MVC given 'back to list' link.this link calls index() method.
Now, I want to call this index() method on Cancel button's Onclick event , how to do this ?
<input type="submit" value="Save" onclick="return validate();" id="btnSubmit" />
<input type="Button" value ="Cancel" onclick="Index()"style=" font-size:"1.2em"/>
What changes I have to make in above code?
You could use the window.location.href to redirect to a specified url:
<input type="Button" value="Cancel" onclick="window.location.href='#Url.Action("index")'" style=" font-size:"1.2em"/>
or simply use an anchor instead of a button:
#Html.ActionLink("Cancel", "Index")

Resources