'ASP.NET Core MVC - posting to a different action name does not bind the values

I'm using a regular html form instead of @html.BeginForm and I have these 2 form tags in my Create.cshtml view file.

I was experimenting with routing, but my post doesn't seem to get the values even if I bind the properties. I've tried in vain but I can't seem to make this work, and can't find the answer from googling.

Create.cshtml

 @model Actor
 @{
    ViewData["Title"] = "Add";
 }

 <section class="container-xl justify-content-center col-lg-5 col-md-8 col-sm-10">
    <div class="row">
        <span class="text-center mb-3">
            <h5>Add a new record</h5>
        </span>
        <div>
            <form>
                <div asp-validation-summary="ModelOnly" class="text-danger"></div>
            </form>
            <div class="form-group">
                <label class="form-label" asp-for="ProfilePictureUrl">Profile Picture</label>
                <input class="mb-2 form-control" type="text" asp-for="ProfilePictureUrl" placeholder="Profile picture" />
            </div>
            <div class="form-group">
                <label class="form-label" asp-for="FullName">Full Name</label>
                <input class="mb-2 form-control" type="text" placeholder="Full name" asp-for="FullName" />
            </div>
            <div class="form-group">
                <label class="form-label" asp-for="Bio">Biography</label>
                <input class="form-control" type="text" placeholder="Bio" asp-for="Bio" />
            </div>
            <form>
                <div class="form-group mt-3">
                    <a class="btn btn-outline-secondary" asp-action="Index">Show All</a>
                    <input asp-action="Create2" class="float-end btn btn-outline-success" type="submit" value="Create" />
                </div>
            </form>
        </div>
    </div>
</section>

Actor.cs

using System.ComponentModel.DataAnnotations;

namespace MovieProject.Models
{
    public class Actor
    {
        [Key]
        public int ActorId { get; set; }
        [Display(Name ="Profile Picture")]
        public string ProfilePictureUrl { get; set; }

        [Display(Name ="Full Name")]
        public string FullName { get; set; }

        [Display(Name ="Biography")]
        public string Bio { get; set; }
    }
}

ActorController.cs

using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;
using MovieProject.Data;
using MovieProject.Data.Services;
using MovieProject.Models;

namespace MovieProject.Controllers
{
    public class ActorController : Controller
    {
        private readonly IActorService _service;

        public ActorController(IActorService service)
        {
            _service = service;
        }

        [HttpPost]
        public IActionResult Create2([Bind("ProfilePictureUrl,FullName,Bio")] Actor actorItem)
        {
            return View("Create");
        }

        public IActionResult Create()
        {
            return View();
        }
    }
}

The methods are getting hit but the post data is null.

Another question is, instead of using MVC convention, can I use a different method name for get and post that is not the same as the view name? How can I get initially load the page for GET using routing that would work in a different view name?

Thanks



Solution 1:[1]

can I use a different method name for get and post that is not the same as the view name?

Yes, you can.

How can I get initially load the page for GET using routing that would work in a different view name?

return to this view.

public IActionResult Create()
        {
            return View("aa");
        }

Below is a work demo, you can refer to it.

In controller:

public IActionResult Create()
    {
        return View();
    }
        [HttpPost]
        public IActionResult Create2(Actor actorItem)
        {
            return View();
        }

Create view:

    @model nnnn.Models.Actor
    
    @{
        ViewData["Title"] = "Create";
    }
    
    <h1>Create</h1>
    
    <h4>Actor</h4>
    <hr />
    <div class="row">
        <div class="col-md-4">
            <form asp-action="Create2">
                <div asp-validation-summary="ModelOnly" class="text-danger"></div>
                <div class="form-group">
                    <label asp-for="ActorId" class="control-label"></label>
                    <input asp-for="ActorId" class="form-control" />
                    <span asp-validation-for="ActorId" class="text-danger"></span>
                </div>
                <div class="form-group">
                    <label asp-for="ProfilePictureUrl" class="control-label"></label>
                    <input asp-for="ProfilePictureUrl" class="form-control" />
                    <span asp-validation-for="ProfilePictureUrl" class="text-danger"></span>
                </div>
                <div class="form-group">
                    <label asp-for="FullName" class="control-label"></label>
                    <input asp-for="FullName" class="form-control" />
                    <span asp-validation-for="FullName" class="text-danger"></span>
                </div>
                <div class="form-group">
                    <label asp-for="Bio" class="control-label"></label>
                    <input asp-for="Bio" class="form-control" />
                    <span asp-validation-for="Bio" class="text-danger"></span>
                </div>
                <div class="form-group">
                    <input type="submit" value="Create" class="btn btn-primary" />
                </div>
            </form>
        </div>
    </div>
    
    <div>
        <a asp-action="Index">Back to List</a>
    </div>
    
    @section Scripts {
        @{await Html.RenderPartialAsync("_ValidationScriptsPartial");}
    }

result:

enter image description here

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1