2

I have an action in my controller:

public PartialViewResult MyAction(int? myId, int? myId2)
{
    MyModel model = new MyModel() { MyId = 10, MyId2 = 20 }
    return PartialView(model);
}

Here is my view:

@model StartSite.Models.Shared.MyModel

@using (Html.BeginForm())
{
    @Html.HiddenFor(m => m.MyId)
    @Html.HiddenFor(m => m.MyId2)

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

Lets call MyAction with params myId=1&myId2=2. But the model is created with different values new MyModel() { MyId = 10, MyId2 = 20 }. And what should be rendered in view? As I expect it should be:

<input id="MyId" name="MyId" type="hidden" value="10">
<input id="MyId2" name="MyId2" type="hidden" value="20">

But in fact the result is:

<input id="MyId" name="MyId" type="hidden" value="1">
<input id="MyId2" name="MyId2" type="hidden" value="2">

As I guess the Html.HiddenFor takes values not from my model but from Reauest.QueryString which is myId=1&myId2=2 at the moment the view is rendered.

Why it happens? Is it expected behaviour?

UPDATE 1: I've edited my question to be more clear.

Serg
  • 6,742
  • 4
  • 36
  • 54

2 Answers2

0

to have access to the Model in the submit try with this

   [HttpPost]
   public virtual PartialViewResult MyAction(MyModel  model)
   {
     //MyModel model = new MyModel();

     // if (myId != null)
     //    model.MyId= myId;
     // else if (myId2 != null)
     //   model.MyId2= myId2;

    //now you have access to your model values
    return PartialView(model);
 }
  • 1
    check this http://stackoverflow.com/questions/4837744/hiddenfor-not-getting-correct-value-from-view-model – Daniel Gpe Reyes Oct 07 '14 at 14:40
  • I understand the idea for post cases, but don't understand why it works this way in my case with get-action. I define value in model but the view takes them from query string. Anyway thank you for pointing the way to fix it. – Serg Oct 07 '14 at 14:59
  • I mean ModelState, not query string. – Serg Oct 07 '14 at 15:07
0

This is expected behavior. Firstly, an Action without any Attributes is automatically a HttpGet. Next, your Action expects a value for 'myId'. This means, if the url calling your Action has a querystring that matches, it'll accept the value. Finally, the value your Action accepts is case-insensitive. Therefore, you do not need to manually set your model values. You can simply do this:

public virtual PartialViewResult MyAction(MyModel model)
{
    return PartialView(model);
}

So when you go to your url, e.g. localhost/myaction?myId=2, model.MyId will be set to 2.

If you do NOT want your model set by a querystring, you must change your Action to not accept any values.

mnsr
  • 12,337
  • 4
  • 53
  • 79
  • Let's not talk about post-action. My question is about @Html.HiddenFor(m => m.MyId2) in the view, which renders myId2 from request values, not from model. – Serg Oct 07 '14 at 07:27
  • This is how it works. GET is a 'post-action' just as much as POST is. If you pass in a querystring with matching values, it's automatically a GET. To get around this, you need to create another action. One that doesnt accept a querystring, and one for your "post-action". – mnsr Oct 08 '14 at 00:39