C# MVC.Net核心表单,强类型表单,不工作(404错误)
我对C非常陌生。我只是想让MVC表单工作起来,以了解信息是如何从视图传递到控制器的。我正在尝试提交表单,并最终使用输入更新页面,这样我就可以知道如何将表单数据传递到XML文件。但当我点击表单上的submit时,出现了一个错误。需要帮忙吗。我找不到一个简单的教程来指导我完成这项工作 我的文件是:User.cs model、addacustomecontroller.sc controller和AddACustomer.cshtml视图 型号:C# MVC.Net核心表单,强类型表单,不工作(404错误),c#,asp.net-mvc,webforms,strongly-typed-view,C#,Asp.net Mvc,Webforms,Strongly Typed View,我对C非常陌生。我只是想让MVC表单工作起来,以了解信息是如何从视图传递到控制器的。我正在尝试提交表单,并最终使用输入更新页面,这样我就可以知道如何将表单数据传递到XML文件。但当我点击表单上的submit时,出现了一个错误。需要帮忙吗。我找不到一个简单的教程来指导我完成这项工作 我的文件是:User.cs model、addacustomecontroller.sc controller和AddACustomer.cshtml视图 型号: using System; using System.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using System.ComponentModel.DataAnnotations;
using System.Web;
namespace WebApplication10.Models
{
public class User
{
[Required]
[Display(Name = "First Name")]
public string FirstName { get; set; }
[Required]
[Display(Name = "Last Name")]
public string LastName { get; set; }
[Required]
[Display(Name = "Role")]
public string Role { get; set; }
}
}
控制器:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using System.Web;
using WebApplication10.Models;
namespace WebApplication10.Controllers
{
public class AddACustomerController : Controller
{
public IActionResult Index()
{
return View();
}
[HttpPost]
public ActionResult UserForm(Models.User person)
{
string firstname = person.FirstName;
string lastname = person.LastName;
string role = person.Role;
ViewBag.n = person.FirstName;
ViewBag.l = person.LastName;
ViewBag.r = person.Role;
return View("UserForm");
}
}
}
视图:
风景图片:
点击表单上的提交按钮时出错的图片:
问题是
return View("UserForm");
对您来说,保留所有称为索引的路由可能是最简单的。你可以
Index()
Index(Models.User)
否则,您需要创建另一个名为UserForm.cshtml的.cshtml视图。谢谢BillRob。我尝试了一些基于您输入的变体,但当我提交URL时,URL从到。您还需要更改您的表单帖子@使用Html.BeginFormIndex、AddACustomer、FormMethod.postThank Bill-你知道除了//docs.microsoft.com之外还有什么好的教程吗?没有Vynce,我没有任何其他建议。微软的网站比几年前好多了。
Index()
Index(Models.User)