문제

My controller:

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

My model:

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

My AddEstablishment View:

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

Result:

enter image description here

My textbox is prepopulating with the Guid default value

00000000-0000-0000-0000-000000000000

How can i avoid it?

Please note there are obvious reasons why I'm passing an instance of the model to the view.

도움이 되었습니까?

해결책

Shouldn't this work?

 public Guid? EstablishmentId { get; set; }

Just ensure that the value is null when you initially display your view.

다른 팁

You don't need to pass the model into the view, it will work if you don't pass it

If you really have to, do this:

private string _establishmentIdAsString = string.Empty;
[Display(Name = "Establishment ID")]
public string EstablishmentIdAsString
{
    get
    {
        return _establishmentIdAsString;
    }
    set
    {
        _establishmentIdAsString = value;
        EstablishmentId = new Guid(value);
    }
}
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top