MVC @ html.textbox用于预先填充Guid默认值

时间:2013-02-28 14:26:14

标签: asp.net-mvc asp.net-mvc-3 asp.net-mvc-4 guid html.textboxfor

我的控制器:

 [HttpGet]
 public ActionResult AddEstablishment()
 {     
    return View("AddEstablishment",new EstablishmentModel());
 }

我的模特:

public class EstablishmentModel
{
    [Display(Name = "Establishment ID")]
    public Guid EstablishmentId { get; set; }
    .............

我的AddEstablishment视图:

 @Html.TextBoxFor(x => x.EstablishmentId, new { @id = "inputEstGuid", @class = "input input-xlarge", @placeholder = "5C3B1CBC-2574-4E2A-A9FA-A8CA0041AB86" })

结果:

enter image description here

我的文本框预先填充了Guid默认值

00000000-0000-0000-0000-000000000000

我该如何避免?

请注意,我将模型实例传递给视图的原因很明显。

2 个答案:

答案 0 :(得分:4)

不应该这样吗?

 public Guid? EstablishmentId { get; set; }

初次显示视图时,请确保该值为空。

答案 1 :(得分:1)

您不需要将模型传递到视图中,如果您不传递它,它将起作用

如果你真的需要,请这样做:

private string _establishmentIdAsString = string.Empty;
[Display(Name = "Establishment ID")]
public string EstablishmentIdAsString
{
    get
    {
        return _establishmentIdAsString;
    }
    set
    {
        _establishmentIdAsString = value;
        EstablishmentId = new Guid(value);
    }
}