-2

我想将值插入到 db。

我的模型是

public bool Create(string userName) {
        _userNA = userName;
        if (validate()) {
            using (DatabaseCommaned cmd = new DatabaseCommaned()) {
                cmd.CommandText = "INSERT INTO tUser(UNA, FNAID, MNAID, LNAID, Email, MobCountryCode, Mobile, ST, LN, ExpDT) VALUES(@una, @fnaid, @mnaid, @lnaid, @email, @mobCode, @mobile, @st, @ln, @exp)";
                cmd.Parameters.AddWithValue("@una", userName);
                ....


                _userID = cmd.ExecuteInsertAndGetID();
                if (_userID > 0) {
                    copyPropertiesInternally();
                    Logs.LogEvent(LogEvent.UserCreated, userName);
                    ResetPassword();
                    return true;
                }
                else {
                    _userNA = string.Empty;
                    return false;
                }
            }
        }
        else 
            return false;
    }

谁能告诉控制器是如何为上述模型编写的

4

2 回答 2

1

创建一个 ViewModel 类,例如 CreateUserViewModel

public class CreateUserViewModel
{
   //add properties reflecting the fields you want to capture in your HTML form

   [Required]
   public string UserName { get; set; }

   //... etc
}

然后创建你的控制器

public ActionResult CreateUser(CreateUserViewModel model)
{
   if(ModelState.IsValid)
   {
      string username = model.UserName;
      //... etc

      bool created = MyUserModelClass.Create(username, ...);
      if(created)
          Return RedirectToAction("Index");
      else
          return View(model); //return the form
   }
   else
   {
      return View(model); //return the form
   }
}
于 2013-06-20T06:14:00.823 回答
1

不是 100% 确定你在问什么,但我会试一试:

public ActionResult Create(string UserName)
{
    var model = new User();
    var created = model.Create(UserName);
    if(created)
       return View();
    else
       return RedirectToAction("Index");
}

根据评论编辑

你说你有模特。它应该看起来像这样:

public class UserModel
{
   public string UserName {get;set}
   public string Email {get;set}
   public string FirstName {get;set}
}

您的控制器操作应该是:

public ActionResult Create(UserModel model)
{
    if (validate(model) {
         var user = new User();
         user.Create(model);
         return View();
    }
    else
       return RedirectToAction("Index");
}

您的数据库插入代码应该是:

public void Create(UserModel model) {
        _userNA = model.UserName;
        using (DatabaseCommaned cmd = new DatabaseCommaned()) {
        cmd.CommandText = "INSERT INTO tUser(UNA, FNAID, MNAID, LNAID, Email, MobCountryCode, Mobile, ST, LN, ExpDT) VALUES(@una, @fnaid, @mnaid, @lnaid, @email, @mobCode, @mobile, @st, @ln, @exp)";
        cmd.Parameters.AddWithValue("@una", userName);
        //....


        _userID = cmd.ExecuteInsertAndGetID();
        if (_userID > 0) {
              copyPropertiesInternally();
              Logs.LogEvent(LogEvent.UserCreated, userName);
              ResetPassword();
              return true;
        }
        else {
              _userNA = string.Empty;
              return false;
        }
    }
}

这只是您提供的零碎信息的指导方针。希望它能让你走上正确的道路。

于 2013-06-20T06:11:01.383 回答