2

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
  • 113
  • 1
  • 2
  • 10

4 Answers4

3

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.

MNF
  • 687
  • 9
  • 13
2

There are many ways to use link button in rezor ,try any of these <button type="button" onclick="@("window.location.href='" +@Url.Action("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 !!!");
    }
});
1

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.

Community
  • 1
  • 1
Shyju
  • 214,206
  • 104
  • 411
  • 497
  • already I have a httppost action - [HttpPost] public ActionResult ApReport(ApReport Ddl) { .....}, how I will get the submitted value within this httppost – ronibd Mar 31 '16 at 14:59
  • 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. – Shyju Mar 31 '16 at 15:04
  • 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 – ronibd Mar 31 '16 at 15:13
  • 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](http://stackoverflow.com/questions/20333021/asp-net-mvc-how-to-pass-data-from-view-to-controller/20333225#20333225). – Shyju Mar 31 '16 at 15:18
1

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