Turi
Turi

Reputation: 171

MVC5 model not recory the text value of textbox

I don't know what I'm doing bad.

Controller:

public class PictureController : Controller
{
    // GET: Picture
    public ActionResult Index()
    {
        return View();
    }

    public ActionResult AddGroup()
    {
        ModelPhotoGroup p = new ModelPhotoGroup();
        return View(p);
    }
    [HttpPost]
    public ActionResult AddGroup(ModelPhotoGroup p)
    {
        return View(p);
    }
}

In AddGroup of HttpPost I get a p.GroupName = null but I'm writing text in the textbox

View:

@model Boda.WebUI.Models.ModelPhotoGroup

@{
    ViewBag.Title = "";
}

<h2>Crear grupo de fotos</h2>
<div class="row">
    <div class="col-lg-6 col-lg-offset-3 col-md-6 col-md-offset-3 col-sm-8 col-md-offset-2">
        <div class="well">
            @using (Html.BeginForm())
            {
                @Html.HiddenFor(x => x.GroupName)
                <div class="form-group">
                    @Html.LabelFor(x => x.GroupName)
                    @Html.TextBoxFor(x => x.GroupName, new { @class = "form-control" })
                    @Html.ValidationMessageFor(x => x.GroupName)
                </div>
                <div class="btn-group">
                    <input type="submit" value="Guardar" class="btn btn-success" />
                    @Html.ActionLink("Cancelar y volver a la lista de Fotografias", "Index", null, new
                   {
                       @class = "btn btn-default"
                   })
                </div>
            }
        </div>
    </div>
</div>

This is the view. Only I have one TextBoxt to write the name of group

HTML Generated:

<form action="/Picture/AddGroup" method="post"><input id="GroupName" name="GroupName" type="hidden" value="">                <div class="form-group">
                <label for="GroupName">GroupName</label>
                <input class="form-control" id="GroupName" name="GroupName" type="text" value="">
                <span class="field-validation-valid" data-valmsg-for="GroupName" data-valmsg-replace="true"></span>
            </div>
            <div class="btn-group">
                <input type="submit" value="Guardar" class="btn btn-success">
                <a class="btn btn-default" href="/Picture">Cancelar y volver a la lista de Fotografias</a>
            </div>

Upvotes: 2

Views: 118

Answers (2)

John Louie Dela Cruz
John Louie Dela Cruz

Reputation: 669

It's because you did not specify the action and controller, add the name of action and controller in your form

@using(Html.BeginForm("AddGroup","Picture"))

Upvotes: 0

user3559349
user3559349

Reputation:

You have included

@Html.HiddenFor(x => x.GroupName)

before

@Html.TextBoxFor(x => x.GroupName)

The value of the hidden input will be whatever the default value of GroupName was when you initialized the model (i.e. null)

When you POST, the DefaultModelBinder sets the value of property GroupName to the first value it reads from the form data (that of the hidden input) and ignores any subsequent values with the same name, so the property is always null.

Remove the hidden input and your model will be correctly bound with the value in the textbox.

Upvotes: 1

Related Questions