0

初学者的困境。在萤火虫中显示移动和国家代码值的错误。我的脚本文件

$(function () {    
    $('#edit-form').submit(function () {
        member = {
        FirstName: $('#f-name').val(),
        Middlename: $('#m-name').val(),
        LastName: $('#l-name').val(),
        CountryCode: $('#mobcc-name').val(),
        Mobile: $('#mob-name').val(),
        Languages: $('#Language').val(),
        Email: $('#email-name').val()
        };
        if ($('#edit-form input').length = 0) { $('#message').html('<strong>Error : </strong><span style="color: #e2442b;">Please fill the required fields</span>'); return; }
        else { $('#message').html('<strong>Processing : </strong><span>Saving information. Please wait...</span>'); }
        $('#edit-form .wrap').children('*:not(label)').attr('disabled', true);
        $.ajax({
            url: "/Account/Edit",
            type: 'POST',
            data: member,
            success: function (d) {
                if(d.s){
                    $('#message').html('<strong>Success : </strong><span class="success_msg">Your information saved</span>');
                    setTimeout(function () { $('#message').children().fadeOut(); }, 5000);
                }
                else {
                    $('#message').html('<strong>Error : </strong><span class="error_msg">' + d.m + '</span>');
                }
                $('#data .ip_wrap').children('*:not(label)').attr('disabled', false);
            },
            error: function () {
                $('#message').html('<strong>Error : </strong><span style="class="error_msg"">A server error occured. Please try after some time. Inconvenience regretted</span>');
                $('#edit-form .wrap').children('*:not(label)').attr('disabled', false);
            }

        });
    });
    $('#cancel').click(function () { if (document.referrer != '') { history.back(); } else {  location.href = "/Account/Index"; } });

});

我的视图文件(表格)

@model Just.In.Models.User

@{
    ViewBag.Title = "Edit User";
}
@section head{<link href="~/Content/Style/Account/Edit.css" rel="stylesheet" />
     <script src="~/Content/Scripts/Account/edit.js"></script>}
<div class="wrapper">
<div class="content">
        <div class ="top-banner">
         <p class="pageTitle">Edit @Model.FullName Profile</p>         
        </div> 
    <p class="helpText"><i>Make changes to the profile</i></p>
        <form id ="edit-form" action ="javascript:void(0)">
            <section class="group">
                <p class= "u-name wrap"><label class ="lbl">User Name</label><input id="u-name" class="input" type="text" value="@Model.UserName" disabled/></p>
                <p class="f-list wrap"><label class ="lbl">First Name</label><input id="f-name" class="input" type="text" value="@Model.FirstName" /></p>
                <p class="m-list wrap"><label class ="lbl">Middle Name</label><input id="m-name" class="input" type="text" value="@Model.Middlename" /></p>
                <p class="l-list wrap "><label class ="lbl">Last Name</label><input id="l-name" class="input" type="text" value="@Model.LastName" /></p>
                <p class="mob-list wrap"><label class ="lbl">Mobile Number</label><input id="mobcc-name" class="input" type="text" value="@Model.CountryCode" />        
                <input id="mob-name" class="input wrap" type="text" value="@Model.Mobile" /></p>
                <p class="lan-name wrap "><label class="lbl">Language</label><span class ="input">@Html.DropDownListFor(o => o.Language, (IEnumerable<SelectListItem>)ViewBag.Languages)</span></p>
                <p class="e-list wrap"><label class ="lbl">e-mail</label><input id="email-name" class="input" type="text" value="@Model.Email" /></p>
            </section>

              <p class="wrap snd-form"><input id="send" class="sbt" type="submit" value="Save" /><input id="cancel" class="sbt" type="button" value="Cancel" /></p>

       </form>
    <div class ="message"></div>
        <div class="error-list" hidden>
        <h2 class="pageTitle">Access Denied</h2>
        <p class="helpText">You don't have enough permission to edit this profile.</p>
        </div>
    </div>
</div>

和我的控制器,我不确定这是否正确!

public JsonResult Edit(string FirstName, string Middlename, string LastName, short CountryCode, int Mobile, Language Languages, string Email)
    {
        Models.User users = new User { FirstName = FirstName, Middlename = Middlename, LastName = LastName, CountryCode = CountryCode, Mobile = Mobile, Language = Languages,Email =Email };
        if (users.Save())
        {              
          return Json(new { s = users.Status == UserStatus.Active, m = "Not changed", id = users.UserID });
        }
        else  return Json(new {s = false, m = "You don't have enough permission to edit this profile" });
    }

和我的保存方法

 public bool Save() {
            bool updated = false;
            DatabaseCommaned cmd = new DatabaseCommaned();
            cmd.CommandText = "UPDATE tUser SET";
            if (_firstName != FirstName) { 
                cmd.CommandText += " FNAID = @fnaid,";
                cmd.Parameters.AddWithValue("@fnaid", string.IsNullOrEmpty(FirstName) ? DBNull.Value : (object)Names.GetName(FirstName).NameID);
                updated = true;
            }
            if (_middleName != Middlename) {
                cmd.CommandText += " MNAID = @mnaid,";
                cmd.Parameters.AddWithValue("@mnaid", string.IsNullOrEmpty(Middlename) ? DBNull.Value : (object)Names.GetName(Middlename).NameID);
                updated = true;
            }
            if (_lastName != LastName) {
                cmd.CommandText += " LNAID = @lnaid,";
                cmd.Parameters.AddWithValue("@lnaid", string.IsNullOrEmpty(LastName) ? DBNull.Value : (object)Names.GetName(LastName).NameID);
                updated = true;
            }
            if (_email != Email) {
                cmd.CommandText += " Email = @email,";
                cmd.Parameters.AddWithValue("@email", Email);
                updated = true;
            }
            if (_countryCode != CountryCode) {
                cmd.CommandText += "MobCountryCode = @ccode, ";
                cmd.Parameters.AddWithValue("@ccode", CountryCode);
                updated = true;
            }
            if (_mobile != Mobile) {
                cmd.CommandText += " Mobile = @mob,";
                cmd.Parameters.AddWithValue("@mob", Mobile);
                updated = true;
            }
            if (_status != Status) {
                cmd.CommandText += " ST = @st,";
                cmd.Parameters.AddWithValue("@st", Status);
                updated = true;
            }
            if (_language != Language) {
                cmd.CommandText += " LN = @ln,";
                cmd.Parameters.AddWithValue("@ln", Language);
                updated = true;
            }
            if (_expiry != Expiry) {
                cmd.CommandText += " ExpDT = @exp,";
                cmd.Parameters.AddWithValue("@exp", Expiry);
                updated = true;
            }

            if (updated == true) {
                cmd.CommandText = cmd.CommandText.TrimEnd(',');
                cmd.CommandText += " WHERE UID = @uid";
                cmd.Parameters.AddWithValue("@uid", _userID);
                updated = cmd.ExecuteNonQuery() == 1;
                if (updated) {
                    copyPropertiesInternally();
                    Logs.LogEvent(LogEvent.UserUpdated, _userNA);
                }
            }
            return updated;
        }

FireBug 中的错误

Server Error in '/' Application.
The parameters dictionary contains a null entry for parameter 'Mobile' of non-nullable type 'System.Int32' for method 'System.Web.Mvc.JsonResult Edit(System.String, System.String, System.String, Int16, Int32, PASKAN.Web.Security.Models.Language, System.String)' in 'PASKAN.Web.Security.Controllers.AccountController'. An optional parameter must be a reference type, a nullable type, or be declared as an optional parameter.
Parameter name: parameters 
4

1 回答 1

1

'Mobile' 属性不可为 null,因此您应该在客户端检查它,因为它作为 null 传递给服务器

于 2013-06-24T07:31:24.393 回答