Q:

Drop down initiated when click on enter on validated text box

I've created a MVC5 App which was generated from my model by scaffold the view and controller,
In the model I have a drop down for dev and prod which is working fine, I have validation
for name for alpha-numeric and the problem is for e.g. user chooses prod (the second 
option)  and enters incorrect data in the name field and presses enter, the view is refreshed and 
the drop down changes the selection from prod to dev. How can I avoid that?
How can I ask in the view if the type is different than dev, if so put the selected item?
Model:
public class Ad
{
    public int ID { get; set; }

    [RegularExpression(@"^[a-zA-Z0-9]*$", ErrorMessage = "Invalid Name")]
    public string Name { get; set; }

    public IEnumerable<SelectListItem> Type
    {
        get
        {
            return new[]
            {
                new SelectListItem {Value = "D", Text = "Dev"},
                new SelectListItem {Value = "p", Text = "Prod"}
            };
        }
   }

View:
   <div class="form-group">
        @Html.LabelFor(model => model.SystemType, new { @class = "control-label col-md-2" })
        <div class="col-md-10">
           @Html.DropDownListFor(model => model.Type, Model.Type)
        </div>
    </div>

Edit the create operation
@model WebApplication3.Models.Ad

<script src="http://code.jquery.com/jquery-1.11.0.min.js"></script>
<script type="text/javascript">
        $(document).ready(function () {
            debugger;

            $('select[name="Type"]').change(function () {
                if ($(this).val() === 'p') {
                    $('input[name="User"]').prop("disabled", true);
                    $('input[name="Password"]').prop("disabled", true);
                }
                else {
                    $('input[name="User"]').prop("disabled", false);
                    $('input[name="Password"]').prop("disabled", false);
                }
            });
        });
    </script>

@using (Html.BeginForm()) 
{
    @Html.AntiForgeryToken()

    <div class="form-horizontal">
        <h4>Ad</h4>
        <hr />
        @Html.ValidationSummary(true)

        <div class="form-group">
            @Html.LabelFor(model => model.Name, new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                @Html.EditorFor(model => model.Name)
                @Html.ValidationMessageFor(model => model.Name)
            </div>
        </div>
        <div class="form-group">
            @Html.LabelFor(model => model.Type, new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                @Html.DropDownListFor(model => model.Type, Model.Type)
            </div>
        </div>
        <div class="form-group">
            @Html.LabelFor(model => model.User, new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                @Html.EditorFor(model => model.User)
                @Html.ValidationMessageFor(model => model.User)
            </div>
        </div>
        <div class="form-group">
            @Html.LabelFor(model => model.Password, new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                @Html.EditorFor(model => model.Password)
                @Html.ValidationMessageFor(model => model.Password)
            </div>
        </div>
        <div class="form-group">
            <div class="col-md-offset-2 col-md-10">
                <input type="submit" value="Create" class="btn btn-default" />
            </div>
        </div>
    </div>
}

<div>
    @Html.ActionLink("Back to List", "Index")
</div>

@section Scripts {
    @Scripts.Render("~/bundles/jqueryval")
}

 public class AdController : Controller
 {
        private AdDBContext db = new AdDBContext();

        public ActionResult Index()
        {
            return View(db.Ad.ToList());
        }

        public ActionResult Details(int? id)
        {
            if (id == null)
            {
                return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
            }

            Ad ad = db.Ad.Find(id);
            if (ad == null)
            {
                return HttpNotFound();
            }
            return View(ad);
        }

        public ActionResult Create()
        {
            return View( new Ad());
        }

        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult Create([Bind(Include="ID,Name,User,Password")] Ad ad)
        {
            if (ModelState.IsValid)
            {
                db.Ad.Add(ad);
                db.SaveChanges();
                return RedirectToAction("Index");
            }

            return View(ad);
        }

        public ActionResult Edit(int? id)
        {
            if (id == null)
            {
                return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
            }

            Ad ad = db.Ad.Find(id);
            if (ad == null)
            {
                return HttpNotFound();
            }
            return View(ad);
        }

        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult Edit([Bind(Include="ID,Name,User,Password")] Ad ad)
        {
            if (ModelState.IsValid)
            {
                db.Entry(ad).State = EntityState.Modified;
                db.SaveChanges();
                return RedirectToAction("Index");
            }
            return View(ad);
        }

        public ActionResult Delete(int? id)
        {
            if (id == null)
            {
                return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
            }
            Ad ad = db.Ad.Find(id);
            if (ad == null)
            {
                return HttpNotFound();
            }
            return View(ad);
        }

        [HttpPost, ActionName("Delete")]
        [ValidateAntiForgeryToken]
        public ActionResult DeleteConfirmed(int id)
        {
            Ad ad = db.Ad.Find(id);
            db.Ad.Remove(ad);
            db.SaveChanges();
            return RedirectToAction("Index");
        }

        protected override void Dispose(bool disposing)
        {
            if (disposing)
            {
                db.Dispose();
            }
            base.Dispose(disposing);
        }
    }
}

A:

On dropdown change event write the selected option value in a hidden field and when page refreshed get the value from hidden field and set the selected item from the action.
$('select#Type').change(function(){

$('#selectedOne').val($(this).val());

});

and in you action read from FormCollection and set selected item of the SelectList
Create Hidden Field in view:
<div class="form-group">
        @Html.LabelFor(model => model.SystemType, new { @class = "control-label col-md-2" })
        <div class="col-md-10">
           @Html.DropDownListFor(model => model.Type, Model.Type)
          <input type="hidden" id="selectedOne" vlaue="" name="selectedOne" />
        </div>
    </div>

In your action:
[HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult Create([Bind(Include="ID,Name,User,Password")] Ad ad,FormCollection form)
        {
            string selectedValue = form["selectedOne"].ToString();
            if (ModelState.IsValid)
            {
                db.Ad.Add(ad);
                db.SaveChanges();
                return RedirectToAction("Index");
            }

            foreach(var item in ad.Type)
            {
              if(item.Value == selectedValue)
              {
                 item.Selected = true;
              }
            }

            return View(ad);
        }

Solution 2:
Another simple solution is to use @Ajax.BeginForm Hepler instead of @Html.BeginForm, so that form posted via ajax, and your dropdown will not get reset.

