convert actionlink into a href in asp mvc - asp.net-mvc

I want to use
#Html.ActionLink("buyer", "buyer", new { Email = Model.Owner.Email })
as a link and role of button.
if it does not have 3rd part I could handle it as a
<a class="btn btn-danger" href="buyer" >new buyer</a>
but because of
new { Email = Model.Owner.Email }
I do not know how can I change <a ....> to work correctly.

You can use the Action Link like this
#Html.ActionLink("Your Link Text", "Your Action", new { Email = Model.Owner.Email }, new { #class = "btn btn-danger"})

Put This and you can use it like that.
#Html.ActionLink("buyer", "buyer", new { #class="btn btn-danger"}, new{ Email = Model.Owner.Email })

Related

where to add new {target = "_blank"} in the link #Html.ActionLink ASP.NET MVC

I need to open the link in new page when click the link but where to add the code
new {target = "_blank"}
in this link ,
<td>#Html.ActionLink("Result", "MasterDetails", new { id = item.LabOrders.ORDER_ID }, new { #class = "btn btn-primary" })</td>
It's part of the htmlArguments, after the route value. You don't necessarily put the whole thing in; you merge with other html arguments, such as #class:
<td>
#Html.ActionLink("Result", "MasterDetails",
new { id = item.LabOrders.ORDER_ID},
new { #class = "btn btn-primary", target="_blank" })
</td>

How do I make an MVC ActionLink call a POST method?

I have an HTML.ActionLink that is acting perfectly except it is not hitting the POST method.
#Html.ActionLink("Join Now!", "JoinFleet", "Members", new { ID =Model.ID , fleet = 1 }, new { #class = "btn btn-primary" })
it results with the following URL as expected.
http://localhost:64096/Members/JoinFleet/2403?fleet=1
I need it to work exactly like this but, hit my POST JoinFleet method.
I toyed with a few ways I could accomplish this task. I was also trying to pass the value of a checked radiobutton to a using as a param.
#using (Html.BeginForm("JoinFleet", "Members", new { fleet = 3 },
FormMethod.Post, new { #class = "form-group" }))
and then submit with a standard submit button
I would be very happy with either making that Action hit my post method in my controller or passing the value of the radio button as the param value for fleet in my using.
When creating a link to a controller action in ASP.NET MVC, using the generic ActionLink method is preferable, because it allows for strongly typed links that are refactoring friendly.
Default: ActionLink
#Html.ActionLink("Delete", "Delete", new { id = item.ID })
Using Button:
<input type="button" title="Delete" value="D" onclick="location.href='#Url.Action("Delete", "movies", new { id = item.ID })'" />
Using Image:
<a href="#Url.Action("Delete", "movies", new { id = item.ID })" title="Edit">
<img src="../../Content/Images/Delete.png" />
</a>

How to get value selected in #Html.DropDownListFor inside the view and pass it to action in OTHER controller?

I have a dropdown list. If user chooses to click Fetch call I want to pass id of selected Call to action FetchCall in controller Person.
<p>
#Html.DropDownListFor(m => m.CallsToMake, new SelectList(Model.CallsToMake.OrderByDescending(call => call.TimeStamp), null), new { #class = "btn btn-default dropdown-toggle", style = "width: 500px;" })
</p>
<p>
#Html.ActionLink("Fetch call","FetchCall", "Person", new { id = HERE_SELECTED_CALL_ID }, new { #class = "btn btn-info btn-lg" })
</p>
View is strongly typed against: #model WebApplication2.Models.ApplicationUser and is associated with Action Details in controller ApplicationUsers.
How to get value selected in #Html.DropDownListFor inside the view and pass it to action in OTHER controller in my case?
If it is not a problem I feel better with java script(I understand better what is going on).
EDIT
This is solution(mine) which lacks getting value from dropdown in java script function.
The dropdown list:
#Html.DropDownListFor(m => m.CallsToMake, new SelectList(Model.CallsToMake.OrderByDescending(call => call.TimeStamp), null), new { #class = "btn btn-default dropdown-toggle", style = "width: 500px;" })
The button when clicked javascript:FetchCall() is invoked:
<input type="button" id="FetchCallButton" value="Fetch call" onclick="javascript:FetchCall()" />
The JavaScript function:
function FetchCall() {
var url = '#Url.Action("FetchCall, "Person")';
$.post(url, { callToMakeId: SELECTED_CALL_IN_DROPDOWN_ID_HOW_TO_GET_IT}, function (data) {
alert('YES');
});
}
The action method in PersonController:
public ActionResult FetchCall(int callToMakeId) {...}
Question: How to get selected call.Id in the java script function?
EDIT 2
Thanks to Tommy's solution I've been able to read selection in dropdown list. The problem is that I get exacly what is written in the dropdown list which is result from ToString() inside CallToMake class.
function:
function FetchCall() {
alert($("#CallsToMake").val());
var url = '#Url.Action("FetchCall", "Person")';
$.post(url, { callToMakeId: $("#CallsToMake").val() }, function (data) {
alert('YES');
});
}
I show in image below contents of the drop down and alert alert($("#CallsToMake").val());:
The only solution that comes to my mind is to display id of the call in the dropdown list and then cut it out in the controller action. It sounds bad, isn't it?
EDIT 3
The list finally looks like:
#Html.DropDownListFor(m => m.CallsToMake, new SelectList(Model.CallsToMake.OrderByDescending(call => call.TimeStamp), "Id","Id"), new { #class = "btn btn-default dropdown-toggle", style = "width: 500px;" })
java script:
function FetchCall() {
alert($("#CallsToMake").val());
var url = '#Url.Action("FetchCall", "Person")';
$.post(url, { callToMakeId: $("#CallsToMake").val() }, function (data) {
alert('YES');
});
}
=
and the Action method
public ActionResult FetchCall(int callToMakeId) {
CallToMake callToMake = db.CallsToMake.Find(callToMakeId);
int idPersonToCall = callToMake.Callee.Id;
db.CallsToMake.Remove(callToMake);
db.SaveChanges();
Debug.WriteLine("I AM HERE");
return RedirectToAction("Details", new { id = idPersonToCall });
}
Action method is being invoked but I am not redirected anywhere. Method just runs and that is all.
First you need to construct your SelectList using the overload that accepts a DataValueField and TextValueField. Refer documentation. This might look something like
... new SelectList(Model.CallsToMake, "CallID", "CallName"), ..
Script (this assumes you remove the onclick attribute from the button)
$('#FetchCallButton').click(function() {
var url = '#Url.Action("FetchCall, "Person")';
var callID = $("#CallsToMake").val();
window.location.href = url + '/' + callID; // redirect to /Person/FetchCall/3
});
It should be straight-forward. The model property name will be the id of the element Razor generates - like this:
#Html.DropDownListFor(m => m.CallsToMake, new SelectList(Model.CallsToMake.OrderByDescending(call => call.TimeStamp), null), new { #class = "btn btn-default dropdown-toggle", style = "width: 500px;" })
would generate
<select id="CallsToMake" name="CallsToMake" class="btn btn-default dropdown-toggle" style="width: 500px">
<option value="1">Some person</option>
</select>
Combining this knowledge with the jQuery call to get an element by id - $("#elementId"), we can then send the selected value back using an AJAX request.
function FetchCall() {
var url = '#Url.Action("FetchCall, "Person")';
$.post(url, { callToMakeId: $("#CallsToMake").val()}, function (data) {
alert('YES');
});
}
thanx for #Tommy it works for me. but I had a problem about if I had to return from two dropdowlists how to do it in query. then I found a way and I think it will help to others.
function fetch(){
var url = '#Url.Action("resultView","Test")';
$.post(url, { LocationID: $("#location_ID").val(), TypeID:$("#type_ID").val() }, function (data) {
alert('YES');
});
}
this works for me fine.

#Html classes for creating modal links

I know how to create ActionLinks. I'm curious (to keep everything simiarl, if I can use the #HTML extensions to create a modal based link.
<a class="btn default" data-toggle="modal" href="#responsive">Create Board
</a>
#Html.ActionLink("Edit Board", "EditBoards", "Forum", new { forumID = Request.QueryString["forumID"] }, new { #class = "btn default" })
The first link above how would I write that with Razor?
#Html.ActionLink("Create Board", null, null, new { #class = "btn default", data_toggle = "modal", href = "#responsive" })
Data attributes can be added by replacing the hyphen "-" with an underscore "_".
or better yet, go grab bootstrab mvc package from nuget, it's got all the helper methods to create bootstrap elements for your mvc view
http://www.nuget.org/packages/Twitter.Bootstrap.MVC/

Enter multiple criteria in a search engine ASP. MVC3

I have the following code which looks into the "ProductCatalog" and returns items if found.
I need to edit the code in order for me to be able to enter multiple values or even special characters like >, <, and, or.
Is this possible?
<div class="search">
#*#using (Html.BeginForm("Search", "ProductCatalog", FormMethod.Get, new { name = "quickSearchform" }))*#
#using (Html.BeginForm("Search", "ProductCatalog", FormMethod.Get, new { name = "searchform", id = "searchform", onsubmit = "return validateSearch();" }))
{
#Html.TextBox("criteria", "Search", new
{
#class = "searchbox",
onfocus = "if (this.value==this.defaultValue) {this.value = ''}",
onblur = "if (this.value=='') {this.value = this.defaultValue}"
})
<a class="btn btnStyleC btn-search quickSearch" href="javascript:searchSumbitOnClick();">Search</a> #*<a class="btn btnStyleC btn-search" href="javascript:searchSumbitOnClick();">Search</a>*# } </div>

Resources