James
James

Reputation: 1392

Post model to an MVC ActionResult

I have an ASP.net application with a [HttpGet] for a view an ActionResult named 'Create' on a Controller called Students

My 'Student' is then posted to the following controller.

[HttpPost]
public async Task<ActionResult> Create(Student student)
{
    Student.Add(student);
    if (ModelState.IsValid)
    {
        var result = db.Students.Add(student);
        await db.SaveChangesAsync();
        return Details(result);
    }
    return View(new CreateStudent());
}

return Details(result); is the line I'm interested in.

Previously I had RedirectToAction where I passed in the result. Id property and used a GET request to query a database.

But, I'm not wanting to have a URL like 'students/details/id=123' Or 'students/details/123' I want to Post my model to the controller 'students/details'

var result is a single 'Student'and my 'Details' ActionResult looks like this:

[HttpPost]
public async Task<ActionResult> Details(Student student)
{
   //
}

but return Details(result); doesn't work, I receive an error message stating:

'The model item passed into the dictionary is of type 'LinkApp.Models.Student', but this dictionary requires a model item of type 'LinkApp.Models.DTOs.CreateStudent'.'

But I believe this is because my URL never goes to '/Students/Details', my URL still shows '/Students/Create'

Any help is appreciated. And again, if it's not clear, please ask for any clarity

Thanks

EDIT DUE TO ME NOT BEING VERY CLEAR ;)

Upvotes: 2

Views: 9106

Answers (1)

maxshuty
maxshuty

Reputation: 10662

So I'm a little bit confused with what you're trying to achieve.

Here is what I've gathered: You want to POST the data. Right now you have a GET method. You can think of a GET method like something that retrieves, and a POST as something that gives.

So your GET method should not be trying to POST anything.

Your GET method should looks like it should be a POST since you're trying to "give" the data to something, not get the data.

[HttpPost]
public async Task<ActionResult> Create(Student student)
    {
        Student.Add(student);
        if (ModelState.IsValid)
        {
            var result = db.Students.Add(student);
            await db.SaveChangesAsync();

            return View("Details", result);
        }

        return View(); //You should add a property to the model called ErrorMessage or something like that, then you could do student.ErrorMessage = "Model state was not valid";, then you could do return View(student); and in the view you could do something like @if (Model.ErrorMessage != null) { @Html.DisplayFor(m=>m.ErrorMessage); }
    }

This is what your post should look like, you're saving the Student model to the database and returning to a view.

Now your GET should just be something like this:

[HttpGet]
public ActionResult Create()
{
return View();
}

Edit:

If you wanted your post to do the error message I left in the comment above you would just do this right above the second return View();

student.ErrorMessage = "Model state was not valid;" //Or whatever you want to say
return View(student);

Then in your view you would have something like this:

@if (student.ErrorMessage != null)
{
 @Html.DisplayFor(m => m.ErrorMessage);
}

Upvotes: 1

Related Questions