0

モデル

public partial class TblMusteriler { 
    public TblMusteriler() { 
       this.TblSayaclar = new HashSet<TblSayaclar>(); 
    } 
    public System.Guid sno { get; set; } 

    [Display(Name = "Müşteri No")] 
    [Required(ErrorMessage = "Müşteri numarası boş geçilemez.")] 
    public string musteri_no { get; set; } 

    [Display(Name = "Müşteri Adı")] 
    [Required(ErrorMessage = "Müşteri adı boş geçilemez.")] 
    public string musteri_adi { get; set; }

コントローラ

public ActionResult SayacEkle()
{
    var musteriler = entity.TblMusteriler.Select(x => new { x.sno, x.musteri_adi });
    ViewBag.musteri_id = new SelectList(musteriler.AsEnumerable(), "sno", "musteri_adi");

    return ContextDependentView(new TblSayaclar());
}

意見

@Html.DropDownList("sno", (SelectList)ViewBag.musteri_id, "--Müşteri Seçiniz--")

HTML出力

<select data-val="true" data-val-number="The field sno must be a number." data-val-required="The sno field is required." id="sno" name="sno" class="input-validation-error"><option value="">--Müşteri Seçiniz--</option>

sno タイプを数値に変更しないでください。GUID タイプである必要があります。ドロップダウンリストの値として guid タイプを使用するにはどうすればよいですか?

ありがとう。

4

1 に答える 1

1

エラー メッセージをオーバーライドする場合は、null 許容 Guid と Required 属性を使用できます。

[Required(ErrorMessage = "Please select a valid SNO")]
public System.Guid? sno { get; set; }

また、既定のメタデータ プロバイダーに null 許容型の Required 属性を暗黙的に追加させたい場合は、AddImplicitRequiredAttributeForValueTypesプロパティApplication_Startを falseに設定できます。

DataAnnotationsModelValidatorProvider.AddImplicitRequiredAttributeForValueTypes = false;
于 2012-07-09T12:58:42.640 に答える