-1

I expect to receive the value of the button to a.aaa

but i get null instead

a.aaa needs to be "stackOverflow" string in [HttpPost]

controller code

 public class DefaultController : Controller
        {
            // GET: Default
            public ActionResult Index()
            {
                return View();
            }

            [HttpPost]
            public ActionResult Index(Class1 a)
            {
                return View();
            }
        }

model

namespace WebApplication3.Models
{
    public class Class1
    {
        public string aaa { get; set; }
    }
}

view

@model WebApplication3.Models.Class1

@{
    ViewBag.Title = "Index";
}

<h2>Index</h2>
@using (Html.BeginForm())
{
    @Html.AntiForgeryToken()
    @Html.ValidationSummary(true)
     <button type="button" name="aaa" id="aaa" value="stackOverflow">pressMe</button>
     <input type="submit" />
}
Nica
  • 177
  • 4
  • 17
david
  • 57
  • 2
  • 8
  • 1
    Your code as posted here is fine (tested it in my project and the value of property `aaa` is `stackOverflow` on postback). –  Nov 02 '14 at 23:50
  • i change one line i ment to write – david Nov 02 '14 at 23:51
  • `button` is not an control that posts back! –  Nov 02 '14 at 23:53
  • and why i need to do Request["aaa"] in input type this exmple work and the value insert into model without Request["aaa"] – david Nov 02 '14 at 23:54
  • so how i pass button value and give it a name like button html control, value of input control is also is name – david Nov 02 '14 at 23:58
  • @david: Why are you trying to pass a button value through your `ActionResult`? – grovesNL Nov 03 '14 at 00:02
  • Its unclear what you are asking. If you want to post a value, use an `` (type = text, hidden, submit, radio etc), a ` –  Nov 03 '14 at 00:06
  • i have 7 buttons and i want to know which buttons was pressed if someone pressed 2 buttons i change the color of the button i ask in javascripit which buttons changed there color and i add the text label of the button to his value' i expect to get button label on the server side to know which buttons pressed – david Nov 03 '14 at 00:07
  • That not what you question says. Edit it with the real code to explain what your trying to achieve. (I suspect its a case of adding a hidden input with `name="aaa"` and updating the value of it using javascript) –  Nov 03 '14 at 00:10

3 Answers3

0

What is [HttpPost] ?

You should find the value in Request["aaa"]

@using (Html.BeginForm("Index", "DefaultController ", FormMethod.Post))
{
    <p>
        @Html.TextBox("aaa", "stackOverflow")
        <input type="submit" value="Text" />
    </p>
}

This code should work for sure, you can try it !

mybirthname
  • 17,949
  • 3
  • 31
  • 55
0

This works correctly for me. Set a breakpoint at the return View(); line in the ActionResult Index(Class1 a) method.

This should demonstrate that a contains a value of stackOverflow for it's property aaa.

If you cannot replicate this behavior, please clarify the behavior that you would expect.

Breakpoint

grovesNL
  • 6,016
  • 2
  • 20
  • 32
0

I recommend implementing via javascript/ajax

In your html's body

<a href="javascript:void(0)" onclick="myJsFunc('aaa');">Press Me</a>

In your html's script

function myJsFunc(somedata) {
    $.ajax({
        type: 'POST',
        contentType: 'application/json',
        url: '/DefaultController/Index',
        data: { 'somedata' : somedata },
        success: function(result) {
                     //do something
                 }
        error: function (errorData) {alert(errorData);}
    });
};

In your controller

[HttpPost]    
public ActionResult Index(string somedata)
{
      if(somedata == "aaa")
      {
            //do something
      }
      return Json("successData");
 }

Then style links as buttons with css.

Or replace your buttons with inputs and follow these examples

Community
  • 1
  • 1
SILENT
  • 3,916
  • 3
  • 38
  • 57