【发布时间】:2015-01-08 18:49:19
【问题描述】:
我制作了一个简单的表单,您可以在其中编写一个字符串,然后将其打印到另一个视图上,但现在我必须检查字符串的长度(使用 jquery)并将其打印到 Jquery UI 对话框中,已经找到了一些教程,但我是这个 MVC4 ASP .Net 的新手,所以在这个框架上使用 jquery(我必须这样做)有点令人困惑。
这是表单的视图:
@{
ViewBag.Title = "Página principal";
}
<h3>Formulario</h3>
@using (Html.BeginForm())
{
@Html.Label("Escribe lo que quieras")<br />
@Html.TextArea("text")<br />
<button type="submit">Enviar</button>
}
这是我打印从第一个视图获得的字符串的视图:
@{
ViewBag.Title = "Hola, Escribiste: ";
}
<hgroup class="title">
<h1>@ViewBag.Title</h1>
<h2>@ViewBag.Message</h2>
</hgroup>
<script type="text/javascript">
var myLength = $("#text").val().length;
</script>
这里是控制器:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
namespace MvcApplication10.Controllers
{
public class HomeController : Controller
{
[HttpGet]
public ActionResult Index()
{
ViewBag.Message = "Modifique esta plantilla para poner en marcha su aplicación ASP.NET MVC.";
return View();
}
[HttpPost]
public ActionResult Index(string text)
{
TempData["Text"] = text;
return RedirectToAction("About", "Home");
}
public ActionResult About()
{
ViewBag.Message = TempData["Text"];
return View();
}
public ActionResult Contact()
{
ViewBag.Message = "Página de contacto.";
return View();
}
}
}
请帮帮我!
【问题讨论】:
-
看起来用javascript而不是通过服务器上的MVC来做这件事要容易得多。在打开 jQuery 对话框的代码中,您可以收听其
open事件,然后使用普通 jQuery 查找字符串长度的放置位置,并使用.html()或其他方式插入它。如果您发布打开 jQuery 对话框的代码以及需要显示长度的位置,我们可能会为您提供帮助。 -
无论您使用的是 MVC、WebForms 还是静态 HTML,使用 jQuery 都没有什么不同,因此 MVC 方面在很大程度上是无关紧要的。你能找出你在编写 JavaScript 时遇到的问题吗?
-
你得看一下Textarea的id。通常 asp.net id 是分层构建的,具体取决于您生成的 dom,例如
ìd="parentid_parentid_textareaid"以保持唯一性。
标签: c# jquery asp.net asp.net-mvc asp.net-mvc-4