duckmike
duckmike

Reputation: 1036

Asp.net MVC 5 not calling controller create method

I am pretty new to MVC and I am trying to create an application from scratch.

My controller code -

    // GET: /Jobs/Create
    public ActionResult Create()
    {
        return View();
    }

    // POST: /Jobs/Create
    [HttpPost]
    [ValidateAntiForgeryToken]
    //public ActionResult Create([Bind(Include="JobId,CategoryId,Title,Location,Duration,Skills,Description,DatePosted")] Job job)
    public ActionResult Create(String title)
    {
        if (ModelState.IsValid)
        {
            //db.Jobs.Add(job);
            //db.SaveChanges();
            //return RedirectToAction("Index");
        }

        //return View(job);
        return View();
    }

and my view code -

@using (Html.BeginForm()) 
{
    @Html.AntiForgeryToken()

    <div class="form-horizontal">
        @Html.ValidationSummary(true)

        <div class="form-group">
            @Html.LabelFor(model => model.Title, new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                @Html.EditorFor(model => model.Title)
                @Html.ValidationMessageFor(model => model.Title)
            </div>
        </div>

        <div class="form-group">
            <div class="col-md-offset-2 col-md-10">
                <input type="submit" value="Create" class="btn btn-default" />
            </div>
        </div>
    </div>
}

when I click "Create" it goes to the create method without any parameters. How do I get it to go to the one with a 'title' parameter?

Upvotes: 1

Views: 1169

Answers (1)

JanR
JanR

Reputation: 6130

Also you are expecting a string on the Create(String Title) action. You would want to change this to whatever the model in your view is.

Unfortunately you didn't post either of these in your code examples. But assuming your view is strongly typed it should have a using something.something.Model at the top of the view, this is will be what you post back to your controller. So your action should be something like: public ActionResult Create(<<MODEL_NAME>> formData)

Upvotes: 1

Related Questions