2011-11-08 44 views
14

私は3つのレコードを持っています(int-(telefon = d.telefon)、decimal-(pesel = d.pesel)、decimal-(nip = d.nip)他のレコードは文字列です。暗黙的に型 'int?'を変換できません。 to int '

public ActionResult detail(int LoginID) 
{ 
    var user = (from d in baza.uzytkowniks 
       where LoginID == d.LoginID 
       select new uzytkownikModel { 
        imie = d.imie, 
        nazwisko = d.nazwisko, 
        telefon = d.telefon, 
        pesel = d.pesel, 
        nip = d.nip, 
        email = d.email, 
        adres_zamieszkania = d.adres_zamieszkania}).ToList(); 

    ViewBag.daneuser = user; 
    return View(); 
} 

そして、私はエラーがあります: '?小数' で

Cannot implicitly convert type 'int?' to 'int'. An explicit conversion exists (are you missing a cast?)

と2別のエラーを代わりに 'int?'

モデル:

public class uzytkownikModel 
    { 
    [Required] 
    public string imie { get; set; } 

    [Required] 
    public string nazwisko { get; set; } 


    public decimal pesel { get; set; } 
    public decimal nip { get; set; } 

    public string adres_zamieszkania { get; set; } 
    public int telefon { get; set; } 
    public string email { get; set; } 

} 

のみimieとnazwiskoが、残りnonnullableあるがnull

を許可している///////////////////// ///////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////// //////////

現在作業中です。私はモデルに変更します

decimal to decimal? 

int to int? 

はあなたが決定的な答えを得るために、より多くのコードをポストする必要がありますが、どこかの変数のいずれかよ、あなたに助け

+0

なぜuzytkownikModelクラスを表示しないのですか?フィールドやプロパティの値が同じ型と一致しないため、おそらくあなたはそのエラーを受け取ります。 –

+0

あなたは 'd'のタイプの定義を投稿できますか? – JohnD

+0

もっとコードを追加しました – user1031034

答えて

15

、NULL可能である特性をとして宣言されなければならない「int型?」または "小数?" 「int」と「decimal」の代わりに使用します。

26

のためのみんなに感謝しますnull可能な型に割り当てるには、.Valueを実行する必要があります。例えば

あなたのオブジェクトはD'sのプロパティimieNullable<int>あり、そしてそれはあなたの問題を引き起こしているものです場合、あなたはこれを行うことができます:

imie = d.imie.Value 

ます。またd.imieがある場合のために監視する必要がありますヌル。例:

imie = d.imie.HasValue ? d.imie.Value : 0 

(最初のnull以外の値に評価される)オペレータ:

imie = d.imie ?? 0 

(私は一例としてd.imieを使用しています - 正確な問題をpintpointする掲示十分なコードがありません。)

+0

.Valueで作業していません...もっとコードを追加しました。 – user1031034

+0

d.imie.Valueはd.imieのように私のために働いた?? 0; // どうもありがとう – Catto

3

int?がありますNullable<int>。データベース列はnullである可能性があるため、マップされたプロパティはNullableです。

int?の値をint個の変数に割り当てるこのコードを検討してください。あなたのuzytkownikModelクラスで

int? w = 2; 
int? x = null; 
int y = w ?? 3; 
int z = x ?? 4; 
関連する問題