以下是一个简单的示例,展示如何创建一个编辑个人资料视图(EditProfile.cshtml)和处理它的控制器动作(ProfileController.cs)。
@model YourNamespace.Models.ProfileModel
@using (Html.BeginForm("EditProfile", "Profile", FormMethod.Post))
{
    @Html.AntiForgeryToken()
    
        @Html.LabelFor(model => model.Name)
        @Html.TextBoxFor(model => model.Name, new { @class = "form-control" })
    
    
        @Html.LabelFor(model => model.Email)
        @Html.TextBoxFor(model => model.Email, new { @class = "form-control" })
    
    
        @Html.LabelFor(model => model.Address)
        @Html.TextBoxFor(model => model.Address, new { @class = "form-control" })
    
    
}
namespace YourNamespace.Models
{
    public class ProfileModel
    {
        public string Name { get; set; }
        public string Email { get; set; }
        public string Address { get; set; }
    }
}
using System.Web.Mvc;
using YourNamespace.Models;
namespace YourNamespace.Controllers
{
    public class ProfileController : Controller
    {
        // GET: Profile/Edit
        public ActionResult Edit()
        {
            // 在此处获取用户的个人资料数据,比如从数据库中查询
            ProfileModel profile = new ProfileModel
            {
                Name = "John Doe",
                Email = "john.doe@example.com",
                Address = "123 Main St"
            };
            return View(profile);
        }
        // POST: Profile/Edit
        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult Edit(ProfileModel profile)
        {
            if (ModelState.IsValid)
            {
                // 在此处保存用户的个人资料数据,比如更新数据库
                // ...
                return RedirectToAction("Index", "Home"); // 保存成功后重定向到首页或其他页面
            }
            // 如果模型验证失败,返回带有错误信息的视图
            return View(profile);
        }
    }
}
这个示例演示了如何创建一个简单的编辑个人资料视图和处理它的控制器动作。在视图中,我们使用了 @Html 帮助器来生成文本框和标签,并使用了 Html.BeginForm 来创建一个表单。在控制器中,我们通过 GET 请求在编辑动作中获取个人资料数据,并通过 POST 请求在编辑动作中保存和更新个人资料数据。
                    上一篇:编辑个人资料后无法保存更改显示
                
下一篇:编辑个人资料视图的创建