1

I have simply view:

<input type="text" id="wprowadz" name="imie"/>
<input type="button" value="Create" onclick="location.href='@Url.Action("Wynik", "Home", new { name = name })'" />

And i need pass text from input to controller:

public string Wynik(string name)
    {
        return name;

    }

i was search on tons of sites - i found this but it's not working - i got error on second "name" in view... what im doing wrong...

3
  • 1
    Why are you using location.href in an onclick handler instead of just having that button submit a form? That seems to be the root of the problem here. Just wrapping your input and button in a form and having the button submit the form would be simpler. Commented May 4, 2018 at 17:39
  • Wrap this inside a form and set name attribute equal to "name" . You will get the value in the controller.. Commented May 4, 2018 at 17:40
  • because @Html.ActionLink("Add", "Wynik", "Home", new { name = "name" }) not working... Commented May 4, 2018 at 17:43

1 Answer 1

1

You seem to be over-complicating this by involving JavaScript at all. If you just want an input, a button, and to post that value to the server then what you want is a form:

@using (Html.BeginForm("Wynik", "Home")
{
    <input type="text" name="name" />
    <input type="submit" value="Create" />
}

Upon clicking the "Create" button, the form is submitted to the Wynik controller action with a string in the name parameter.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.