【问题标题】:how to get partial view input field value by id using jQuery in asp.net mvc如何在asp.net mvc中使用jQuery通过id获取部分视图输入字段值
【发布时间】:2018-04-17 12:33:21
【问题描述】:

我有一个剃须刀视图,其中一部分使用部分视图填充登录用户的手机号码

<div class="form-group">

                <div class="col-md-10">
                    @Html.Action("ShopOwnerEditMobilePartial", "ManageShop", new { appUserId = Model.ApplicationUserId })

                </div>
                <br />
                <div class="form-group">
                    <div class="col-md-offset-2 col-md-10">
                        <input type="button" value="Save Changes" onclick="updateMobile()" class="btn btn-primary btn-block pull-right" />
                    </div>
                </div>
            </div>

这是控制器代码

public ActionResult ShopOwnerEditMobilePartial(string appUserId)
    {
        Repository rep = new Repository();
        ViewBag.MobileNumber= rep.GetMobileNumberByAppUserId(appUserId);
        return PartialView("_ShopOwnerEditMobilePartial");
    }

这是剃刀视图的html代码

<input type="text" class="form-control" maxlength="20" id="MobileBox"
       name="crimeRef" placeholder="Mobile Number"
       value="@(ViewBag.MobileNumber ?? String.Empty)"/>

这是我的 jquery 代码,它无法通过使用 MobileBox Id 获得价值

var CityNewValue="";
function updateCity()
{
    CityNewValue=$('#CityDDL').val();
    $.ajax({
        url: '@Url.Action("updateCity", "ManageShop")',
        method: 'post',
        data: { shopId:@Model.ShopId,NewCityID:CityNewValue},       //the data to be passed to the server
        dataType: 'json',                   // data that we are expecting back from the server
        success: function (data) {         // data variable will contain the data fetched from the server
            $('#successAlert').slideDown();
        },
        error: function (err) {

        }
    });
}

问题来了,当我尝试获取手机号码的编辑值时,部分视图中定义的 MobileBox id 和 updateCity() 函数都出现了未捕获的异常

【问题讨论】:

    标签: javascript jquery razor asp.net-mvc-5 asp.net-mvc-partialview


    【解决方案1】:

    所有代码都是正确的,我在代码中使用了return 语句,在所需的工作执行之前,这就是为什么我无法到达所需的代码行

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-12-19
      • 2013-12-09
      • 1970-01-01
      • 1970-01-01
      • 2018-02-10
      • 2023-03-14
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多