2017-04-11 26 views
0

我有一個包含用戶名,密碼和代碼的登錄模型。如何自動填寫模型數據

現在我想要如果你填寫代碼,然後自動關聯的用戶名和密碼必須填寫。例如,你填寫代碼:HD然後選項卡,然後用戶名和密碼的textfields必須被填充

的操作方法是這樣的:

[HttpPost] 
     public ActionResult LoginBalieUser(V_LoginModel_BalieUser model) 
     { 

      ISalesPersonProfile salesAgent99 = CommerceFrameworkBase.SalesPersons.GetSalesPerson("HD");   
      var salesAgent = CommerceFramework.ShopAccounts.GetShopAccount(Guid.Parse("3E90BB13-36BA-435D-8ED7-E930A4F8C758"), true);   

      if (model.BalieCode == salesAgent99.Id) 
      { 
        if (!ModelState.IsValid) 
         return View(model); 

       if (!ShopApi.UserState.Login(model.UserName, model.Password, model.RememberMe)) 
       { 
        ModelState.AddModelError("","The username or password is incorrect"); 
        return View(model); 
       } 

      } 



      return RedirectToAction("Index", "Profile"); 
     } 

擴展模型是這樣的:

public class V_LoginModel_BalieUser : LoginModel 
    { 

     public string BalieCode { get; set; } 
} 

和或iginal模式是這樣的:

// 
    // Summary: 
    //  A model to login into the webshop. 
    public class LoginModel 
    { 
     public LoginModel(); 

     // 
     // Summary: 
     //  Gets or sets the password. 
     [AllowHtml] 
     [DataType(DataType.Password)] 
     [Display(Name = "Password")] 
     [Required(ErrorMessageResourceName = "Validation_RequiredField")] 
     [StringLength(30, ErrorMessageResourceName = "Validation_MaxLengthExceeded")] 
     public virtual System.String Password { get; set; } 
     // 
     // Summary: 
     //  Gets or sets a value indicating whether to remember the user to login him automatically 
     //  on the next visit. 
     [Display(Name = "Login_RememberMe")] 
     public virtual System.Boolean RememberMe { get; set; } 
     // 
     // Summary: 
     //  Gets or sets the username. 
     [DataType(DataType.EmailAddress, ErrorMessageResourceName = "Validation_InvalidField")] 
     [Display(Name = "EmailAddress")] 
     [Required(ErrorMessageResourceName = "Validation_RequiredField")] 
     [StringLength(80, ErrorMessageResourceName = "Validation_MaxLengthExceeded")] 
     [TrimAttribute(new[] { })] 
     public virtual System.String UserName { get; set; } 
    } 

和觀點如下:

@{ 

    Layout = LayoutPaths.General; 
} 

@model Sana.Commerce.DomainModel.Account.V_LoginModel_BalieUser 


<div class="semicolumn"> 
    <div class="form-holder"> 
     @using (Html.BeginForm(htmlAttributes: new { @class = "form" })) 
     { 
      @Html.AntiForgeryToken() 

      <table> 
       <tr> 
        <th> 
         @Html.DisplayNameFor(modelItem => modelItem.BalieCode) 
        </th> 
        <th></th> 
       </tr> 

       <tr> 
        <td> 
         @Html.TextBoxFor(modelItem => modelItem.BalieCode) 
        </td> 

       </tr> 



       <tr> 
        <th> 
         @Html.DisplayNameFor(modelItem => modelItem.UserName) 
        </th> 
        <th></th> 
       </tr> 

       <tr> 
        <td> 
         @Html.TextBoxFor(modelItem => modelItem.UserName) 
        </td> 

       </tr> 


       <tr> 
        <th> 
         @Html.DisplayNameFor(modelItem => modelItem.Password) 
        </th> 
        <th></th> 
       </tr> 

       <tr> 
        <td> 
         @Html.TextBoxFor(modelItem => modelItem.Password) 
        </td> 

       </tr> 


      </table> 
      <div class="form-row"> 
       <h4></h4> 
       <input type="submit" value="Login" /> 
      </div> 
     } 
    </div> 
    <div> 

    </div> 
</div> 

@section Scripts{ 
    <script src="~/Scripts/jquery-1.10.2.min.js"></script> 
    <script> 


     $(document).ready(function(){ 
      $("#Id").change(function() { 
       $.ajax({ 
        type: "Get", 
        url: '@Url.Action("loginbalieUser", "profile")', 
        data: { id: $("").val() }, 
        dataType: "json", 
        success: function (data) { 
         $("#UserName").val(data[0]); 
         $("#Password").val(data[1]); 
        } 
       }); 
      }) 


     }); 
    </script>  

    } 

於是,我試着用Ajax調用。但確切的實現我不知道。

謝謝

這是模型:ISalesPersonProfile

// 
    // Summary: 
    //  Interface for the Sales Person Profile entity. 
    public interface ISalesPersonProfile : IEntity 
    { 
     // 
     // Summary: 
     //  Email of this Sales person. 
     string Email { get; set; } 
     // 
     // Summary: 
     //  Sales person's code. 
     string Id { get; set; } 
     // 
     // Summary: 
     //  Job Title of the Sales person. 
     string JobTitle { get; set; } 
     // 
     // Summary: 
     //  Name of the Sales person. 
     string Name { get; set; } 
     // 
     // Summary: 
     //  Phone number of the Sales person. 
     string Phone { get; set; } 
     // 
     // Summary: 
     //  Gets or sets the related customer id list related with the sales person. 
     IList<string> RelatedCustomerIds { get; set; } 
    } 

但我得到這個錯誤:

loginbalieUser:232 Uncaught ReferenceError: ValidateBalieCode is not defined 
    at HTMLInputElement.onchange (loginbalieUser:232) on this line: <tr> 
        <td> 
         @Html.TextBoxFor(modelItem => modelItem.BalieCode, new { @onchange = "ValidateBalieCode()" }) 
        </td> 

       </tr> 

,這是我的Ajax調用:

function ValidateBalieCode(){ 
       var code = $('input#BalieCode').val(); 

        $.ajax({ 
        type: "Get", 
        url: '@Url.Action("ValidateBalieCode", "profile")', 
        data: { "BalieCode":code }, 
        success: function (data) { 

         $("input#UserName").val(data.UserName); 
         $("input#Password").val(data.Password); 
        } 
       }); 


      } 

如果我loa d的頁面在goog鉻:

我看到這一點:

function ValidateBalieCode(){ 
       var code = $('input#BalieCode').val(); 

        $.ajax({ 
        type: "Get", 
        url: '/sitemap.xml', 
        data: { "BalieCode":code }, 
        success: function (data) { 

         $("input#UserName").val(data.UserName); 
         $("input#Password").val(data.Password); 
        } 
       }); 


      } 

OKE,

我有現在這樣的:

function ValidateBalieCode() { 
       var code = $('input#BalieCode').val(); 

       $.ajax({ 
        async: true, 
        type: "Get", 
        url: "/profile/ValidateBalieCode/", 
        contentType: "application/json; charset=utf-8", 
        data: { "BalieCode": code }, 
        success: function (data) { 

         $("input#UserName").val(data.UserName); 
         $("input#Password").val(data.Password); 
        } 
       }); 


      } 

但是URL是這樣的:

http://localhost:5923/en-us/profile/loginbalieuser 

so always always:en-我們。但如果我這樣做:

url: "en-us/profile/ValidateBalieCode/", 

然後在鉻我看到這一點:

http://localhost:5923/en-us/profile/en-us/profile/ValidateBalieCode/?BalieCode=1 404 (Not Found) 
+0

我無法找到ID爲'#Id'任何控制。那麼什麼是'$(「#Id」)。change' – mmushtaq

+0

謝謝你的回覆。我編輯帖子我以爲Id是salesAgent99的ID。我不知道如何用Ajax做到這一點 –

+0

真的很難理解你的問題。您的'LoginBalieUser'操作標有'HttpPost',並且在您的ajax中您指定'get' .. – mmushtaq

回答

0

您需要爲越來越UserNamePasswordBalieCode一個單獨的操作方法。


Demo


查看:

@using (Html.BeginForm("LoginBalieUser", "LogIn", FormMethod.Post, new { @class = "form" })) 
     { 
      @Html.AntiForgeryToken() 

      <table> 
       <tr> 
        <th> 
         @Html.DisplayNameFor(modelItem => modelItem.BalieCode) 
        </th> 
        <th></th> 
       </tr> 

       <tr> 
        <td> 
         @Html.TextBoxFor(modelItem => modelItem.BalieCode, new { @onchange = "ValidateBalieCode()" }) 
        </td> 

       </tr> 



       <tr> 
        <th> 
         @Html.DisplayNameFor(modelItem => modelItem.UserName) 
        </th> 
        <th></th> 
       </tr> 

       <tr> 
        <td> 
         @Html.TextBoxFor(modelItem => modelItem.UserName) 
        </td> 

       </tr> 


       <tr> 
        <th> 
         @Html.DisplayNameFor(modelItem => modelItem.Password) 
        </th> 
        <th></th> 
       </tr> 

       <tr> 
        <td> 
         @Html.PasswordFor(modelItem => modelItem.Password) 
        </td> 

       </tr> 


      </table> 
      <div class="form-row"> 
       <h4></h4> 
       <input type="submit" value="Login" /> 
      </div> 
     } 

控制器:

public class HomeController : Controller 
{ 
    [HttpGet] 
    public ActionResult Index() 
    { 
     return View(new SampleViewModel()); 
    } 


    public JsonResult ValidateBalieCode(string BalieCode) 
    { 
     string strUserName = string.Empty; 
     string strPassword = string.Empty; 

     List<SampleViewModel> _db = new List<SampleViewModel>();   
     _db.Add(new SampleViewModel() { BalieCode = "1", UserName = "test1", Password = "test1" }); 
     _db.Add(new SampleViewModel() { BalieCode = "2", UserName = "Test2", Password = "test2" }); 


     var SampleViewModel = _db.Find(x => x.BalieCode == BalieCode); 
     if(SampleViewModel != null) 
     { 
      strUserName = SampleViewModel.UserName; 
      strPassword = SampleViewModel.Password; 
     } 

     return Json(new{ UserName = strUserName, Password = strPassword }, JsonRequestBehavior.AllowGet); 
    } 
} 

腳本

<script type="text/javascript"> 

     function ValidateBalieCode(){ 
       var code = $('input#BalieCode').val(); 

        $.ajax({ 
        type: "Get", 
        url: '@Url.Action("ValidateBalieCode", "Home")', 
        data: { "BalieCode":code }, 
        success: function (data) { 

         $("input#UserName").val(data.UserName); 
         $("input#Password").val(data.Password); 
        } 
       }); 


      } 

     </script> 
+0

謝謝。非常好。但當然我有數據庫中的數據存儲 –

+0

是的,對於演示我使用了一個List。對於實際情況,您需要從數據庫中找到對應BalieCode的UserName和Password。 – mmushtaq

+0

oke。但是我得到這個錯誤: –