Domanda

The controller file in the application HomeController.cs, defines the two controls SetPowerOff and [HttppPost] SetPowerOff.

public ActionResult SetPowerOff(int ID, string deepness)
    {
     .............
        if (Request.IsAjaxRequest())
        {
            var viewModel = new HomeSetPowerOffViewModel()
            {
                //List of devices
                Devicelist = list,
                age = 18

            };
            return PartialView("_SetPowerOff", viewModel);
        }
        else
        {
            return HttpNotFound();
        }
    }

[HttpPost]
public ActionResult SetPowerOff(HomeSetPowerOffViewModel homeSetPowerOffViewModel)
{
}

The Partial View returns the devices in a list.

@using (Html.BeginForm("SetPowerOff", "Home", FormMethod.Post, new { @class = "form-horizontal" }))
{
    <div class="modal-header">
        <button type="button" class="close" data-dismiss="modal" aria-hidden="true">X</button>
        <h3 id="myModalLabel">Devices information</h3>
    </div>
    <div class="modal-body ">
        <div class="row-fluid">
            <div class="span12">
                <ul>
                @foreach (var devices in Model.Devicelist)
                { <li>
                    @devices.Name;
                  </li>
                }
              </ul>
            </div>
        </div>
    </div>
    <div class="modal-footer">
        @Html.ActionLink("Cancel", "Index", "Home", new { @class = "btn btn-danger" })
        <button type="submit" class="btn btn-success">
            Ok
        </button>
    </div> 
}

After I press the Ok button on the Partial View it goes to the HttpPost request. Here the homeSetPowerOffViewModel received is null and the age is 0. I would very much like to know why is resetting them. Thanks for your time!

È stato utile?

Soluzione

you dont have any inputs in your form so there is nothing to submit

Altri suggerimenti

Your view<form>does not contain the form field like <input><select><textarea> for the age field.

So when you submit, it will be set to default integer value 0

Assuming age property of HomeSetPowerOffViewModel is a type of integer (int)

On post you receive hidden or any other input fields ( dropdowns, textbox and so on ). There is no such fields in your form. ( you are not filling the model in any way )

Althought, you can submit with ajax and you can send whatever you want to your controller

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top