1

I have an action which looks like this:

public ActionResult NewPicture(int id)
{
    var response = service.Get(id);
    if (!response.Succeed)
       return Error(response.MainError.ErrorType);
    return ManagementView("/Gallery/EditPicture.cshtml", new PictureSaveViewModel() { Id = 0, GalleryId = id });
}

In the PictureSaveViewModel the GalleryId refers to parent gallery and the Id is the picture's Id. Because it's NewPicture action, The Id is set to 0 and the GalleryId set to the id parameter. In the View, I have a form that contains multiple inputs for PictureSaveViewModel.

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

Here are two hidden inputs for Id and GalleryId for the specified model.I expect that the hidden filed Id's value to be 0 and GalleryId's value to be the id of the parameter. But when I call the action like this /Gallery/NewPicture/82, both hidden fields value is 82

<input data-val="true" data-val-number="The field Id must be a number." data-val-required="The Id field is required." id="Id" name="Id" type="hidden" value="82">
<input data-val="true" data-val-number="The field GalleryId must be a number." data-val-required="The GalleryId field is required." id="GalleryId" name="GalleryId" type="hidden" value="82">     

I don't know this is happening. I event tried to @Html.Hidden("Id" ,@Model.Id), but still same result. Other input fiels like title, description, .... are working fine.

1 Answer 1

1

In your request, you get the Id parameter. So when you pass the model with that filed name it will automatically used the request id data. So please try the below code, change the id parameter to tid

 public ActionResult NewPicture(int tid)

    {
        var response = service.Get(tid);
        if (!response.Succeed)
           return Error(response.MainError.ErrorType);
        return ManagementView("/Gallery/EditPicture.cshtml", new PictureSaveViewModel() { Id = 0, GalleryId = tid });
    }
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.