0

Is there any possibility to block (by attribute for example) passing URL's parameters into view?

public ActionResult UserForm(Guid? entityId, bool isCopy = false)
{
    ViewBag.IsNewOrCopy = entityId.IsNullOrEmptyOrCopy();

    if (ViewBag.IsNewOrCopy)
        return View(new UserFormViewModel() {Roles= service.GetRoles(entityId, isCopy) });

    return View(service.GetUser(entityId.Value));
}

For example I would like to stop passing entityId parameter because when I update changes via post method the entityId (@Html.HiddenFor(m => m.EntityId)) is also passed via viewmodel although I would like it to be passed as null variable.

krypru
  • 1,692
  • 3
  • 22
  • 29
  • The value of your form input will override the value in the url. –  Nov 15 '17 at 08:15
  • What do you mean you want it passed as `null`? If your `UserFormViewModel` contains a property named `EntityId`, then the value in the input will be set to the value of the `entityId` parameter in your GET method –  Nov 15 '17 at 08:25
  • @StephenMuecke But when I pass new viewmodel (without entityId set) into view, the entityId property is still filled in by URL's property and I wonder if there is possibility to turn it off. – krypru Nov 15 '17 at 08:37
  • 1
    That is a `ModelState` issue (refer the last part of [this answer](https://stackoverflow.com/questions/26654862/textboxfor-displaying-initial-value-not-the-value-updated-from-code/26664111#26664111) which explains the default behavior. The simplest solution is just to name the parameter different from the model property –  Nov 15 '17 at 08:49
  • And you can always use `[Bind(Exclude="EntityId")]UserFormViewModel model` in your POST method signature if you always want it to be `null`, but if that was the case, your view model should not have that property –  Nov 15 '17 at 08:51

0 Answers0