1

First of all, I'm new to web development, so forgive me for this newbie question.

I have 4 inputs on my View, which I would like to send to my controller when a button is pressed.

<input type="text" name="title" />
<input type="text" name="description" />
<input type="text" name="link" />
<input type="date" name="date" />

And here is my ControllerMethod:

public ActionResult AddNewsToList(string title ="a", string description = "b", string link = "c", string pubDate = "01-02-2016")
    {
        Models.RssWriter.createNewsItem(title, description, link, pubDate);

        return View("News", NewsFeed.Models.RssReader.GetRssFeed());
    }

So can anyone guide me to making a button that can do this for me? Thanks for reading.

EDIT

This is how it actually looks like right now:

CreateNews.cshtml

@using (Html.BeginForm("AddNewsToList"))
{
    <table>
        <tr>
            <th>
                Titel
            </th>
        <tr>
            <td>
                <input type="text" name="title" />
            </td>
        <tr>
            <th>
                Beskrivelse
            </th>
        <tr>
            <td>
                <input type="text" name="description" />
            </td>
        <tr>
            <th>
                Link
            </th>
        <tr>
            <td>
                <input type="text" name="link" />
            </td>
        <tr>
            <th>
                Udgivelsesdato
            </th>
        <tr>
            <td>
                <input type="date" name="date" />
            </td>
        <tr>
            <td>
                <br />
            </td>

    </table>
    <input type="submit" id="AddNewsToList" name="AddNewsToList" value="Tilføj" />

}

But this does not seem to work.

9
  • I don't see why this post deserves at negative vote. It is hard to find a similar question on this site. Every question about submitting forms is more advanced, like have two buttons at the same time. Commented Jan 19, 2016 at 18:25
  • what have you tried? do you know anything about HTML, <form> tag? Also why do you have default parameter values defined for your action? Commented Jan 19, 2016 at 18:46
  • The default values are because they are optional Commented Jan 19, 2016 at 18:56
  • 1
    What do you mean by "does not seem to work"? Commented Jan 19, 2016 at 19:02
  • 1
    Do you have your ActionResult decorated with [HttpPost] ? Commented Jan 19, 2016 at 19:03

1 Answer 1

2

You might need to specify the controller name as the second argument to the BeginForm method.

As @Gendolkari also pointed out: "it appears that if you provide the action name, you must also provide the controller name. Otherwise the parameter is interpreted as a RouteValues object. He can probably just use BeginForm() without any parameters."

Compare:

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.