3

I want to pass model to Post method of controller. When method is called it shows null value for content and 0 for Id. Ideally it should contain the values of model it has displayed.

enter image description here

View:

@model MvcApplication4.Models.WorldModel
@{
    ViewBag.Title = "Information";
    Layout = "~/Views/Shared/_Layout.cshtml";
}

@using (Html.BeginForm("Information", "World", FormMethod.Post))
{
    @Html.AntiForgeryToken()
    @Html.ValidationSummary(true)
    <br />
    @Html.DisplayFor(model => model.Id)
    <br />
    @Html.DisplayFor(model => model.Content)
    <br />
    <input type="submit" value="Next" />
}

Controller: Method called when click on submit button.

        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult Information(WorldModel worldModel)
        {
            CandidateSession cs = (CandidateSession)Session["Can"];
            var can = cs.Candidates.Where(x => x.IsNameDispalyed == false);
            if (can.Count() > 0)
            {
                var can1 = can.First();
                can1.IsNameDispalyed = true;
                Session["Can"] = cs;
                return View(new WorldModel() { Id = can1.Id, Content = can1.Name });
            }
            return View(new WorldModel());
        }

Model:

public class WorldModel
    {
        public int Id { get; set; }

        public string Content { get; set; }
    }
user2323308
  • 759
  • 5
  • 16
  • 34

3 Answers3

5

You should add hidden inputs containing your values :

@Html.HiddenFor(model => model.Id)
@Html.HiddenFor(model => model.Content)

This is because the model binder will search for inputs (like textboxes, or hidden fields) to get values and associated them with your model properties (based on input names). No input is created with DisplayFor, so the model binder can't find your values when you submit your form.

Réda Mattar
  • 4,361
  • 1
  • 18
  • 19
  • Thanks for help. I have included the Hidden field for id and content with display field, now values are passed. But old values are not cleared it shows old values even though new values are assigned to id and content field and displayed on screen.why it is so? – user2323308 Nov 08 '13 at 11:18
  • I have to call ModelState.Clear(). To get new values every time. – user2323308 Nov 08 '13 at 11:28
  • Indeed, generally when you return to view from your post action, it means that validation failed and you want to display submitted data in order to correct them. That's why the `ModelState` keeps the old values. When it is successful, you usually redirect to the `GET` action, with some parameters if needed. But you can also clear the `ModelState` if it suits you. – Réda Mattar Nov 08 '13 at 12:34
4

I think that @Html.DisplayFor values are not submitted.

Try including a @Html.HiddenFor for the same values.

user1987392
  • 3,921
  • 4
  • 34
  • 59
0

You can use from @Html.Hidden('#IdComponent') for pass value from .cshtml form to controller as well as:

in .cshtml : in to page have:

@model Owj_SDK.Models.Service.Ticket.Ticket
@Html.Hidden("accntid")

and in body have:

    <a OnClick="testfnc('@Model.accntid')"> </a>

in .js file have:

function referralToColleagueTicketModal(accntide) {
    debugger
    $.get('/Ticket/referralToColleagueTicketModal/', { 'accntid': accntide}, function (html) {
        $('#dvtckdelmodal').html(html);
        $('#dvtckdelmodal').modal('show');
        $('#accntid').val(accntide);
    });
}
Mojtaba Nava
  • 858
  • 7
  • 17