级联下拉列表与Json固定为Guid Id

时间:2016-08-16 18:05:03

标签: c# json asp.net-mvc drop-down-menu

大家好我需要修复级联下拉列表以使用Guid ID ... 它适用于Int Id ...但我需要在桌面上使用Guid Id。

当我将类型更改为Guid时(在我的模型和数据库中)...它不会填充下拉列表

请帮助解决这个问题

我明白了:

控制器

Dim myExcel As Excel.Application
Dim myBook As Excel.Workbook
Dim mySheet As Excel.Worksheet

Set myExcel = CreateObject("Excel.Application")   

Set myBook = myExcel.Workbooks.Add(1)
Set mySheet = myBook.Worksheets(1) 

myExcel.Visible = True   

DoCmd.TransferSpreadsheet acExport, acSpreadsheetTypeExcel12, qrytbl1, mySheet, True   

Set myBook = Nothing
Set mySheet = Nothing
Set myExcel = Nothing

查看

  public JsonResult GetCountries()
    {           
        return Json(countries.GetAll().ToList(), JsonRequestBehavior.AllowGet);            
    }


  public JsonResult GetStatesByCountryId(string countryId)
    {     
//I know, I have to convert to Guid here... the problem is in the first dropdownlist       
        int Id = Convert.ToInt32(countryId);

        var states = from s in state.GetAll() where s.CountryId == Id select s;

        return Json(states);
    }

1 个答案:

答案 0 :(得分:0)

如果您将类型从实体模型/ db中的Id更改为Guid,则以下行将失败

public JsonResult GetStatesByCountryId(string countryId)
{            
    int Id = Convert.ToInt32(countryId);

}

Convert.ToInt32需要一个可以转换为有效int的对象(例如:“123”)。但是Guid会像"49e17a97-88ce-4acc-8aba-ae0c8740fd5d"一样无法转换为int。所以只需使用Guid作为你的参数。

public JsonResult GetStatesByCountryId(Guid countryId)
{ 
    var states = (from s in state.GetAll() where s.CountryId == countryId  
                                                               select s).ToList();   
    return Json(states, JsonRequestBehavior.AllowGet);
}

假设state.GetAll方法返回一个item集合,其中CountryId属性的类型为Guid。 (而且州的身份也是Guids)