2017-07-21 29 views
0

在MVC JsonResult殘疾人專用我有一個模型類是這樣的: -值是不是從角功能

public class InternShip 
{ 
    [Key] 
    public int ID { get; set; } 
    public string Name { get; set; } 
    public string Descr { get; set; } 
    public int Amount { get; set; } 
    public int Duration { get; set; } 
    public DateTime CreatedDate { get; set; } 
    public DateTime ExpiryDate { get; set; } 

} 

現在我已經在我創建NG-click事件的看法: -

<a ng-click="IntrnDetail(r.ID)" style="cursor:pointer"><h5>{{r.Name}}</h5></a> 

在點擊我的價值傳遞給它由功能handeled我的角度文件InternShip.js: -

$scope.IntrnDetail = function (InternID) { 
     window.location.href = '/Home/InternDetail/' + InternID; 
    } 

現在我的控制器(HomeController的)擁有的ActionResult(InternDetail)爲: -

[HttpGet] 
    public JsonResult InternDetail(string InterID) 
    { 
     List<InternShip> Intern = new List<InternShip>(); 
     int IntID = Convert.ToInt32(InterID); 
     using (EBContext db = new EBContext()) 
     { 
      Intern = db.Interns.Where(x => x.ID == IntID).ToList(); 
     } 

     return new JsonResult{ Data= Intern, JsonRequestBehavior=JsonRequestBehavior.AllowGet}; 
    } 

但問題是,雖然值內InternShip.js的InternID但在JsonResult未來(InternDetail)InterID越來越空。我不知道爲什麼會發生這種情況。

請幫助!

回答

1

這裏的問題是,因爲默認的MVC路線的路線是一樣的東西

{action}/{controller}/id(optional) 

意味着,當你通過

"Home/Index/3" 

的路線將id的值設置爲3 現在你的動作結果參數不是id而是interID所以要麼做

window.location.href ='/Home/InternDetail?InterID='+InternID ; 

或改變interIDID

public JsonResult InternDetail(int ID) 

,你還可以配置你的路線

+0

這做到了。謝謝你的解釋。 – Deepak