1

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?

1
  • The code you have shown looks OK. Are you sure the POST method is not being hit Commented Oct 23, 2014 at 23:41

1 Answer 1

1

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)

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

2 Comments

That's not correct. Html.BeginForm() will render <form action="/ControllerName/Create" method="post" novalidate="novalidate"> if no parameters are passed. The parameters are only necessary when you need to override the defaults
true, didn't see the initial page was also called create, it's the signature that's not correct then

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.