You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

ASP.NET MVC视图插空值及[HttpPost]引发404错误的修复咨询

解决方案:ASP.NET MVC 用户注册数据提交问题

问题1:打开视图时数据库自动插入空值

原因:控制器方法未区分GET/POST请求,打开页面的GET请求会触发方法执行;且实体类无必填验证规则,空模型能通过ModelState.IsValid校验,进而执行插入操作。

解决:拆分控制器方法为GET(返回视图)和POST(处理提交)两个独立版本:

// GET 请求:仅返回注册视图,不执行数据库操作
public ActionResult register_page()
{
    ViewBag.Message = "Your contact page.";
    return View(new auth()); // 传入空模型供视图绑定使用
}

// POST 请求:仅处理表单提交逻辑
[HttpPost]
[ValidateAntiForgeryToken] // 与视图中的@Html.AntiForgeryToken()对应,防范CSRF攻击
public ActionResult register_page(auth model)
{
    ViewBag.Message = "Your contact page.";
    
    if (ModelState.IsValid)
    {
        // 为视图未提供的字段设置默认值
        model.abonelik_suresi = DateTime.Now;
        model.abonelik_durumu = "Active"; // 可根据业务需求调整
        
        user_manager.Add(model);
        return RedirectToAction("Index"); // 成功提交后重定向,避免重复提交
    }
    
    return View(model); // 验证失败返回视图,显示错误提示
}

问题2:添加[HttpPost]后出现404 Not Found

原因:仅标记[HttpPost]后,打开页面的GET请求找不到对应的Action方法,导致404错误。

解决:添加上述的GET版本register_page方法,专门处理页面打开的GET请求。

问题3:点击提交按钮仅新增空值记录

原因:

  1. 实体类无必填验证,空模型可通过ModelState.IsValid校验;
  2. 视图验证摘要未显示属性级错误,无法引导用户补全信息;
  3. 模型绑定存在潜在冲突(参数名与类名重复)。

解决:

  1. 给实体类必填属性添加验证特性:
using System.ComponentModel.DataAnnotations;

namespace Entities
{
    public class auth : IEntity
    {
        public int id { get; set; }

        [Required(ErrorMessage = "用户名不能为空")]
        public string user_name { get; set; }
        
        [Required(ErrorMessage = "密码不能为空")]
        public string password { get; set; }
        
        [Required(ErrorMessage = "邮箱不能为空")]
        [EmailAddress(ErrorMessage = "请输入有效的邮箱地址")]
        public string e_mail { get; set; }

        public int phone { get; set; } // 若需必填,添加[Required]并改为int?(允许空值)

        public string abonelik_durumu { get; set; }
        
        public DateTime abonelik_suresi { get; set; }

        public bool situation { get; set; }
    }
}
  1. 修改视图的验证摘要,显示属性级错误:
@Html.ValidationSummary(false, "", new { @class = "text-danger" })
  1. 优化AuthManager上下文一致性(可选):
    若继承的Repository<auth>已实现Add方法,删除authManager中重复的Add方法,避免上下文冲突:
public class authManager : Repository<auth>
{
    DatabaseContext context = new DatabaseContext();

    public List<auth> GetAll()
    {
        return context.kullanicilar.ToList();
    }

    public auth Get(int id)
    {
        return context.kullanicilar.Find(id);
    }

    // 删除重复的Add方法,直接使用基类实现
    /*
    public int Add(auth kullanicilar)
    {
        context.kullanicilar.Add(kullanicilar);
        return context.SaveChanges();
    }
    */

    public int Update(auth kullanicilar)
    {
        context.kullanicilar.AddOrUpdate(kullanicilar);
        return context.SaveChanges();
    }

    public int Delete(int id)
    {
        context.kullanicilar.Remove(Get(id));
        return context.SaveChanges();
    }
}

内容的提问来源于stack exchange,提问作者Berkay Isıkoglu

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.25 03:44:55