【问题标题】:Is it possible to check the textbox value in MVC without clicking submitting/postback是否可以在不单击提交/回发的情况下检查 MVC 中的文本框值
【发布时间】:2014-04-28 13:30:26
【问题描述】:

我是 MVC 新手。

我有一个表格,我们称之为注册表。我有 10 个文本框(id、name、address..etc) 一旦用户输入 ID - 我必须检查 DB 它是否已经可用,然后我需要显示状态。

在 MVC 中有可能吗?不点击提交按钮??

提前致谢。

【问题讨论】:

  • 有可能。签入keypress$.ajax 的jQuery API。您还需要控制器中的端点来检查数据库中已有的重复项。
  • 您知道“MVC”是一种独立于语言的设计模式,而不是框架的名称

标签: javascript jquery asp.net-mvc buttonclick autopostback


【解决方案1】:

是的,实际上实现这一点并不难。您可以在要在服务器上异步验证的模型属性上使用 RemoteAttribute,在您的情况下是 id

http://msdn.microsoft.com/en-us/library/system.web.mvc.remoteattribute(v=vs.118).aspx

// model
public class MyModel
{
   [Remote("ValidateId", "MyController")]
   public string Id { get; set; }
}


// controller
public class MyController
{
  public ActionResult ValidateId(string id)
  {
    // action will be invoked when you change value in the browser.
    // you have to return a string that contains an error if the id fails validation.
    // or true if the id is valid.

    // this is in case id is valid
    // return Json(true, JsonRequestBehavior.AllowGet);

    // this in case id is not vlaid.
    // return Json("id is not valid", JsonRequestBehavior.AllowGet);

  }
}

也看看这个:

http://msdn.microsoft.com/en-us/library/gg508808(v=vs.98).aspx

【讨论】:

  • 不知道该属性。谢谢:)
猜你喜欢
  • 1970-01-01
  • 2020-10-08
  • 2016-08-19
  • 1970-01-01
  • 2013-06-13
  • 2021-06-30
  • 1970-01-01
  • 2020-12-23
  • 1970-01-01
相关资源
最近更新 更多