1

I was trying a basic asp .net mvc project configuration explained below;

Before first submit, the result is left one in photo which is expected, after submit textbox(@Html.TextBoxFor(model => model.Name)) and text(@Model.Name) shows different values as seen in right one in photo, unexpectedly; why is it so? They show different values although their model is unique.

enter image description here

Model:

 public class Personnel
{
    public string Name { get; set; }
}

View:

@model deneme.Models.Personnel

@{
    ViewBag.Title = "Index";
}

<h2>Index</h2>


@using (Html.BeginForm("Index", "Home"))
{
    @Html.TextBoxFor(model => model.Name)

    <br/>
    <br/>

    @Model.Name

    <br/>
    <br/>

    <input type="submit" value="submit" />
}

Controller:

public ActionResult Index(Personnel personnel)
{
     if (string.IsNullOrEmpty(personnel.Name))
     {
         personnel.Name = "Ahmet";
     }
     else
     {
         personnel.Name = personnel.Name + "server";
     }

     return View(personnel);
}
serefbilge
  • 1,654
  • 4
  • 29
  • 55

1 Answers1

1

Edit: My previous answer didn't answer your question.

If you add ModelState.Clear() to the controller action, it will work as you want it to.

public ActionResult Index(Personnel personnel)
{
     if (string.IsNullOrEmpty(personnel.Name))
     {
         personnel.Name = "Ahmet";
     }
     else
     {
         personnel.Name = personnel.Name + "server";
     }

     ModelState.Clear();
     return View(personnel);
}
Vedran
  • 757
  • 6
  • 21