Linkbutton equivalent in MVC C# Razor

11,643

Solution 1

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.

Solution 2

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 !!!");
    }
});

Solution 3

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.

Solution 4

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

Share:
11,643
ronibd
Author by

ronibd

Updated on June 05, 2022

Comments

  • ronibd
    ronibd almost 2 years

    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

  • ronibd
    ronibd about 8 years
    already I have a httppost action - [HttpPost] public ActionResult ApReport(ApReport Ddl) { .....}, how I will get the submitted value within this httppost
  • Dushan Perera
    Dushan Perera about 8 years
    As long as your form field names matches with your property names of ApReport classs, Model binding will take care of mapping the posted form values to the Ddl object.
  • ronibd
    ronibd about 8 years
    I have added in model class (public string linkToSubmit { get; set; }) and inside the class (string lnkBtn; lnkBtn = Request.Form["linkToSubmit"];) but after clicking the link, its not submitting any value to the controller, just back to the same page
  • Dushan Perera
    Dushan Perera about 8 years
    You do not need to add linkToSubmit to your view model class ? You want to pass the form field values to your action method rite ? Also you do not need to read the values like Request.Form . MVC model binding takes care of you. I strongly recommend to some reading on how model binding works and then go further with writing code. Start here.
  • ronibd
    ronibd about 8 years
    I am getting compilation error when adding my model name
  • MNF
    MNF about 8 years
    you must add a reference to the model on the top of your view @model Classes.YourModel