2015-03-19 24 views
-1

我有一個「myfile.cs」文件和「myview.cshtml」文件。枚舉在「myfile.cs」文件中聲明。它們顯示在「myview.cshtml」中,但這些值必須轉到「General.js」文件,並使用.post方法從那裏到另一個「PerformSave.cs」文件。從剃鬚刀文件傳遞枚舉值

所以我想傳遞int值。例如,我想通過PerformSave(1)「法國」。任何想法我該怎麼做?

myfile.cs 
namespace mynamespace.Models 
{ 
public enum country 
{ 
    England, // 0 
    France, // 1 
    Germany // 2 
} 

public country CustCountry { get; set; } 
... 

} 

my.cshtml 
<button id="Save" class="button">Save</button><br> 
<script> 
     $("#Save").click(function() { 
     PerformSave(@Model.CustCountry); 
     } 
</script> 

general.js 
PerformSave: function (country) { 
    $.post(URL , { country },function (data,status, xhr) { 

    } 
} 
+0

不太清楚你想要做什麼。你如何渲染屬性'CustCountry'的控件?什麼是您想要回發的控制器方法。注意@ @ Model.CustCountry'將回傳原始值,而不是在視圖中修改的值。 – 2015-03-19 08:51:21

回答

0

我猜你在哪裏有一個下拉列表,你可以選擇國家。從國家獲得字符串後,將其轉換爲CountryEnum,然後返回到int。

從字符串到枚舉:

var country = (CountryEnum) Enum.Parse(typeof(CountryEnum), "France", true); 

從枚舉到INT:

現在
var value = (int) country; 

值將有國家的ID

+0

謝謝,這有助於... – user1651888 2015-03-19 09:54:31