ASP.NET 核心 MVC - 发布到不同的操作名称不绑定值

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

我使用的是常规 html 表单而不是 @html.BeginForm,我的 Create.cshtml 视图文件中有这 2 个表单标签。

我正在试验路由,但我的 post 似乎没有获得值,即使我绑定了属性。我试过但没有成功,但我似乎无法完成这项工作,也无法通过谷歌搜索找到答案。

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();
        }
    }
}

方法被击中,但 post 数据为空。

另一个问题是,我可以不使用 MVC 约定,而是为 get 和 post 使用与视图名称不同的不同方法名称吗?我如何使用可在不同视图名称中工作的路由来初始加载 GET 页面?

谢谢

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?

return 到此视图。

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

下面是工作demo,大家可以参考一下

在控制器中:

public IActionResult Create()
    {
        return View();
    }
        [HttpPost]
        public IActionResult Create2(Actor actorItem)
        {
            return 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");}
    }

结果: