ronibd
ronibd

Reputation: 113

Linkbutton equivalent in MVC C# Razor

I am converting a web form to MVC C# razor project. I like to know how I will get the same functionality in Razor where it will create a link and sumbit the form into the same page. The web form code is -

<asp:LinkButton ID="lnkBtn_1" runat="server" Text='<%#Eval("Sales") %>' OnCommand="LinkButton1_Command" CommandArgument='<%#Eval("Sales") %>' ></asp:LinkButton>

Thanks in advance

Upvotes: 2

Views: 14215

Answers (4)

Saumil Rami
Saumil Rami

Reputation: 57

if you are new to mvc then you need to check it's basic from MVC tutorials: Please follow below thread for convert your web app code to MVC

https://msdn.microsoft.com/en-us/library/system.web.mvc.html.linkextensions.actionlink(v=vs.118).aspx

@Html.ActionLink("buttonText",new { controller = "ContreollerName", action = "ActionName", @sales = "Your parameter" })

And then make a action result in your controller

Upvotes: 1

There are many ways to use link button in rezor ,try any of these <button type="button" onclick="@("window.location.href='" [email protected]("ActionResult", "Controller") + "'")"> Link Button </button> <a href="@Url.Action("ActionResult", "Controller")"> Link Button </a> @Html.ActionLink("Text","ActionResult","Controller") submitting form into the same page you have to use Ajax Begin Form or use simple json object with a ajax post method. Try like this

$("#btnSave").click(function (e) {

var urlpath = '@Url.Action("ActionResult", "Controller")';
$.ajax({
    url: urlpath ,
    type: "POST",
    data: JSON.stringify({ 'Options': someData}),
    dataType: "json",

    contentType: "application/json; charset=utf-8",
    success: function (data) {
        if (data.status == "Success") {
            alert("Done");

        } else {
            alert("Error occurs on the Database level!");
        }
    },
    error: function () {
        alert("An error !!!");
    }
});

Upvotes: 2

MNF
MNF

Reputation: 717

Try this

@Html.ActionLink("buttonText", "ControllerAction", "YourController ", 
         new { @sales = YourModel.Parameter}, new { @class =yourcssclass" })

Your Controller

public class YourController : Controller
{
public ActionResult Index()
 {
        var model = YourDataToDisplay;

        return View(model);
 }
public ActionResult ControllerAction(int sales)
{
    //.....
}
}

You can use ViewData to define ButtonText.

Upvotes: 3

Shyju
Shyju

Reputation: 218782

You can write an anchor tag. But clicking on the anchor tag usually does not submit the form, but triggers an HTTP GET request. So you need to write some java script to do the form submission.

<form action="Customer/Create">
   <a href="#" id="linkToSubmit">Submit</a>
</form>

Using jQuery and some unobtrusive javascript code to bind the click event on this anchor tag,

$(function(){
  $("#linkToSubmit").click(function(e){
    e.preventDefault(); // prevent the normal link click behaviour (GET)
    $(this).closest("form").submit();
  });
});

Now you can execute the code in your HttpPost action of Create action method in the Customer controller (because that is the form's action set to)

Another option is to keep the submit button inside your form and style it so that it looks like a link as explained in this answer.

Upvotes: 1

Related Questions