POST data to controller with ASP.NET MVC

19,757

Solution 1

Try replacing your code with the following:

@using (Html.BeginForm("FiEdit", "EditConfig", FormMethod.Post))
{
    <div class="form-group">
        <label for="issuerKey">Issuer Key</label>
        <input type="text" name="key" />
        <input type="submit" class="btn btn-default" value="Search" />
    </div>
}

This will POST the parameter key to the EditConfig controller.

If you'd like to post to the action TestEdit in another controller, say the TestController, your code should be changed to the following:

@using (Html.BeginForm("TestEdit", "Test", FormMethod.Post))
...

To resolve the "model item passed into the dictionary" error, change your POST to be this:

[HttpPost]
public ActionResult FiEdit(int key)
{
    return View(new IssuerKey() { Key = key });
}

Solution 2

ou can try with:

    @using (Html.BeginForm(("FiEdit", "EditConfigController", FormMethod.Post,
                new { enctype = "multipart/form-data" })))
{
    <div class="form-group">
        <label for="issuerKey">Issuer Key</label>
        <input type="text" name="key" />
        <input type="submit" class="btn btn-default" value="Search" />
    </div>
}
Share:
19,757
Toby Caulk
Author by

Toby Caulk

I work at an eCommerce company that handles large quantities of money on the internet by day. By night, I am an aspiring game developer who loves pixel art and tile/voxel games. My favorite language is Java (or C#!), and I love to design and program games in LibGDX. I have almost half a decade of programming experience by now and my recent venture into the corporate software engineer world will only improve my skills and allow me to acquire a more "meaty" knowledge base.

Updated on June 04, 2022

Comments

  • Toby Caulk
    Toby Caulk almost 2 years

    I am using ASP.NET MVC with C# and pure bootstrap. One of my views contains a label, text input box, and a submit button:

    @{
       ViewBag.Title = "BinSearch";
       Layout = "~/Views/Shared/_LayoutSearch.cshtml";
    }
    
    <h2>BinConfig Search</h2>
    
    @using (Html.BeginForm("FiEdit", "EditConfigController"))
    {
        <div class="form-group">
            <label for="issuerKey">Issuer Key</label>
            <input type="text" name="key" />
            <input type="submit" class="btn btn-default" value="Search" />
        </div>
    }
    

    When I click the "submit" button, I would like to transfer the data to a controller, EditConfigController to this method:

    [HttpPost]
    public ActionResult FiEdit(int key)
    {
        return View(new IssuerKey().Key = key);
    }
    

    Which then is supposed to create a new view where I can edit data based off the key provided. This is the FiEdit view:

    @model BinFiClient.Models.IssuerKey
    
    @{
        ViewBag.Title = "FiEdit";
        Layout = "~/Views/Shared/_LayoutEdit.cshtml";
    }
    
    <h2>FiEdit</h2>
    

    However, when I click the "submit" button, I receive a 404 error, and the URL path looks like this:

    http://localhost:58725/EditConfigController/FiEdit
    

    Which is actually the path to the method in the controller that I posted above.

    What I need is basically a way to POST data to another controller. How can I accomplish this?

    Edit: Now I am receiving the error:

    The model item passed into the dictionary is of type 'System.Int32', but this dictionary requires a model item of type 'BinFiClient.Models.IssuerKey'.

  • Benjamin RD
    Benjamin RD almost 9 years
    @TobyCaulk ups! was an error, but now the code is fixed.
  • Benjamin RD
    Benjamin RD almost 9 years
    Try with: public ActionResult FiEdit() { var key = Request.Params["key"].ToString(); return View(new IssuerKey().Key = key); } The problem looks like how the data is sent to the controller. In the page isn't defined any model to get in your view and send it to the controller again.
  • Toby Caulk
    Toby Caulk almost 9 years
    This seems to have helped, but now I am presented with the error The model item passed into the dictionary is of type 'System.Int32', but this dictionary requires a model item of type 'BinFiClient.Models.IssuerKey'. Why would that be?
  • Toby Caulk
    Toby Caulk almost 9 years
    Never mind, figured it out! Looks like I was accidentally passing in an integer to my view instead of the IssuerKey it required. Thanks!