如何将js变量传递给@ Model.MyModel(MVC4) [英] How to pass js variable to @Model.MyModel (MVC4)

查看:182
本文介绍了如何将js变量传递给@ Model.MyModel(MVC4)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

以下是我的模型

public class AddressBook
{
    public string UserId { get; set; }
    public IList<Address> Addresses { get; set; }
    public AddressBook()
    {
        UserId = "";
        Addresses = new List<Address>();
    }
}

public class Address
{
    public string Company { get; set; }
    public string Title { get; set; }
    public string FName { get; set; }
    ...
 }

控制器使用列表构建AddressBook地址
主页面使用AddressBook模型(@model mymodel.AddressBook),我可以使用Model.Addresses [index]访问不同的地址。

The controller builds the AddressBook with a list of addresses. The main page uses the AddressBook model (@model mymodel.AddressBook) and I can access the different addresses using Model.Addresses[index].

关于第一页显示地址列表,每个地址都带有一个编辑按钮(为了清楚起见,我删除了html代码):

On the page I display the list of addresses each with an Edit button (I stripped the html code off for clarity):

@model mymodel.AddressBook
...
@for (int i = 0; i < Model.Addresses.Count; i++)
{
    @Model.Addresses[i].Company
    @Model.Addresses[i].FName
    ...
    @:<input type="image" src="/images/edit.gif" onclick="addressEdit('@i'); return false;" title="Edit" />

}

当用户点击编辑按钮时,我调用javascript addressEdit并传递所选地址的索引。

When the user clicks on the edit button I call javascript addressEdit and pass it the index of the selected address.

<script type="text/javascript">
    function addressEdit(index) {
        $('#FName').val('@Model.Addresses[index].FName');
        $('#Addr1').val('@Model.Addresses[index].Company');
        ...
    }
</script>

问题在于jQuery行$('#FName')。val('@ Model。地址[指数] .FName');变量索引在VS2012中以红色加下划线,并且消息当前上下文中不存在名称'索引'。

The problem is on the jQuery lines $('#FName').val('@Model.Addresses[index].FName'); Variable index is underlined in red in VS2012 with message "the name 'index' does not exist in the current context".

如何传递'index'上的值来提取我需要的数据?

How do you pass the value on 'index' to extract the data I need?

推荐答案

使用某个类名将元素包装在一个跨度中。将循环中的所有内容包装为div。我也从标记中删除 onclick 方法,因为我们将在不引人注意的方式。

Wrap your elements in a spans with some class name. Wrap everything in the loop a div too. I am also removing the onclick method from the markup because we will do it in the unobtrusive way.

@for (int i = 0; i < Model.Addresses.Count; i++)
{
  <div class='entry'>
     <span class='spanAddress'> @Model.Addresses[i].Company </span>
     <span class='spanFName'> @Model.Addresses[i].FName </span>       
     <img src="/images/edit.gif" class='editLink' />
  </div>
}

现在在你的javascript中

Now in your javascript

$(function(){

  $(".editLink").click(function(e){

     e.preventDefault();
     var _this=$(this);
     var fname=_this.closest("div.entry").find(".spanFName").html();      
     var add=_this.closest("div.entry").find(".spanAddress").html();    

     $('#FName').val(fname);
     $('#Address').val(add);

  });  

});

这篇关于如何将js变量传递给@ Model.MyModel(MVC4)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆