ASP.NET Core MVC-发布到其他动作名称不绑定值

发布于 2025-01-28 09:30:50 字数 3208 浏览 1 评论 0原文

我使用的是常规的HTML表单,而不是@html.beginform,并且在我的create.cshtml视图文件中有这2个表单标签。

我正在尝试路由,但是即使我绑定属性,我的帖子似乎也没有得到值。我徒劳无功,但我似乎无法做这项工作,也找不到谷歌搜索的答案。

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

这些方法被击中,但帖子数据为null。

另一个问题是,我可以使用其他方法名称来获取和发布与视图名称不同的方法名称吗?如何最初加载页面以获取在其他视图名称中使用的路由?

谢谢

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

热风软妹 2025-02-04 09:30:50

我可以使用其他方法名称进行获取和发布不是
与视图名称相同?

是的,你可以。

我如何最初加载页面以获取使用路由
用不同的视图名称工作?

返回此视图。

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

以下是一个工作演示,您可以参考它。

在控制器中:

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

结果:

”在此处输入图像说明”

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文