javascript - Jquery 插件 - 内联编辑..如何?

标签 javascript jquery asp.net-mvc asp.net-mvc-3 razor

我正在尝试在 MVC3 和 Jquery 页面中使用 ToggleEdit。这是链接 http://staticvoid.info/toggleEdit/ ...虽然此页面中有很多演示示例,但我真的不明白如何在 View 中进行此操作。我是 Jquery 和 MVC 的新手。

第 1 步:我在页面顶部引用了 Jquery 插件。

<link href="../../Content/themes/base/toggleEdit.css" rel="stylesheet" type="text/css" />
<script src="../../Scripts/jquery.toggleEdit.min.js" type="text/javascript"></script>         

第 2 步:了解此 Jquery 如何在 HTML.. View 中触发。

  <table>

  @foreach (var item in Model) 

  {

   <tr>
    <td>
        @Html.DisplayFor(modelItem => item.Name)
    </td>

    <td>
        @Html.DisplayFor(modelItem => item.Phone)
    </td>
    </tr>

  }
   </table>

如何更改此 View 代码并使用此 Jquery 插件。感谢您的帮助。单击行或行中的项目(单元格)时,应激活内联编辑。并保存。

这是来自源网站的示例示例。我实际上如何为我的表格 HTML 字段实现类似的东西?

   $(el).find('input,select').toggleEdit({
events: {
    edit: 'mouseenter'
}
   });

最佳答案

这是我为您编写的完整示例,应该能让您走上正轨。

一如既往,您从 View 模型开始:

public class MyViewModel
{
    public string Name { get; set; }
    public string Phone { get; set; }
}

然后一个 Controller 来填充这个 View 模型并将它传递给 View :

public class HomeController : Controller
{
    public ActionResult Index()
    {
        // TODO: Fetch from a repository instead of hardcoding
        var model = Enumerable.Range(1, 10).Select(x => new MyViewModel
        {
            Name = "name " + x,
            Phone = "phone " + x
        });
        return View(model);
    }
}

然后是一个 View (~/Views/Home/Index.cshtml):

@model IEnumerable<MyViewModel>

<table>
    <thead>
        <tr>
            <th>Name</th>
            <th>Phone</th>
        </tr>
    </thead>
    <tbody>
        @Html.EditorForModel()
    </tbody>
</table>

<a id="toggleEdit" href="#">Toggle edit</a>

然后是相应的编辑器模板,它将为我们 View 模型的每个元素呈现(~/Views/Home/EditorTemplates/MyViewModel.cshtml):

@model MyViewModel
<tr>
    <td>@Html.EditorFor(x => x.Name)</td>
    <td>@Html.EditorFor(x => x.Phone)</td>
</tr>

最后是我们需要包含的脚本和样式:

<link href="@Url.Content("~/Content/jquery.toggleEdit.css")" rel="stylesheet" type="text/css" />

<script src="@Url.Content("~/Scripts/jquery-1.5.1.min.js")" type="text/javascript"></script>
<script src="@Url.Content("~/Scripts/jquery-ui-1.8.11.js")" type="text/javascript"></script>
<script src="@Url.Content("~/Scripts/jquery.toggleEdit.js")" type="text/javascript"></script>

<script type="text/javascript">
    $(function () {
        $('#toggleEdit').click(function () {
            $('table :input').toggleEdit();
            return false;
        });
    });
</script>

关于javascript - Jquery 插件 - 内联编辑..如何?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6753509/

相关文章:

javascript - 使用 Chart.js 检测图表部分上方的悬停事件

javascript - 如何在提交Ajax表单后加载html?

javascript - 如何使用 jQuery 禁用单击事件后发生的功能

android - 使用JS替换固定位置

javascript - 将对象从节点传递到前端

JavaScript 参数在箭头函数中使用,但未在本地范围内的任何位置声明 - Twilio 函数

javascript - 如何区分上滚和下滚鼠标事件

asp.net-mvc - 无法复制文件....因为找不到错误

asp.net-mvc - 使用 URL.ACTION 将参数传递给 Controller

asp.net-mvc - RouteCollection 和路由表有什么区别