Przekazywanie Id przez forumalrz

0

Czy dobrze rozumiem aby przekazać Id usera któremu zmieniam avatara muszę stworzyć

HiddenFor(x=>x.Id)

aby przekazć do akcji

ChangeAvatar(string userId)

Wygląda to tak:

        [HttpPost]
        public ActionResult ChangeAvatar(HttpPostedFileBase file, string Id)
        {
            userService.ChangeAvatars(file, Id);
            return RedirectToAction("Index", "Home");
        }        
@model Shop.Models.ApplicationUser
@{
    ViewBag.Title = "ChangeAvatar";
}

<h2>Zmiania Avatara</h2>

<div>
    @using (Html.BeginForm("ChangeAvatar", "Manage", FormMethod.Post ,new { enctype = "multipart/form-data" }))
    {
        @Html.AntiForgeryToken()
        <hr />
        @Html.ValidationSummary("", new { @class = "text-danger" })
        @Html.HiddenFor(x=>x.Id)
        <div class="form-group">
            @Html.LabelFor(m => m.ImageUrl, new { @class = "col-md-2 control-label" })
            <div class="col-md-10">
                <input type="file" name="file" id="file" />
            </div>
        </div>
        <div class="form-group">
            <div class="col-md-offset-2 col-md-10">
                <input type="submit" value="Zmień Avatar" class="btn btn-default" />
            </div>
        </div>
    }
</div>

a kod html wygenerował się tak

<input name="__RequestVerificationToken" type="hidden"
value="lPFovpod3niOuKqMQc5sYtvP1mJIbFhGy84HHLrteQkFV6tjuMx0W0DSzW8-2PGLdewMzaR71ORyup6ABmRzNKAm4CfkX2cCjQXh2fECe3BC9B-cA6yCxkupWT4YtfnjYnWOZlaZOLb_Zo8VKjdmYA2">
1

W formularzu dodałeś kod @Html.AntiForgeryToken(), który wstawia element input typu hidden z tokenem zabezpieczającym przed atakiem Cross-Site Request Forgery. Pokazany przez Ciebie wygenerowany kod HTML jest właśnie tym fragmentem kodu.

Powinieneś poszukać kodu wyglądającego podobnie do tego:

<input name="Id" type="hidden" />

Jeśli używasz AntiForgeryToken, to powinieneś przy metodzie akcji typu POST użyć atrybutu ValidateAntiForgeryTokenAttribute:

[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult ChangeAvatar(HttpPostedFileBase file, string Id)
{
    userService.ChangeAvatars(file, Id);
    return RedirectToAction("Index", "Home");
}        
0
Burmistrz napisał(a):

W formularzu dodałeś kod @Html.AntiForgeryToken(), który wstawia element input typu hidden z tokenem zabezpieczającym przed atakiem Cross-Site Request Forgery. Pokazany przez Ciebie wygenerowany kod HTML jest właśnie tym fragmentem kodu.

Powinieneś poszukać kodu wyglądającego podobnie do tego:

<input name="Id" type="hidden" />

Jeśli używasz AntiForgeryToken, to powinieneś przy metodzie akcji typu POST użyć atrybutu ValidateAntiForgeryTokenAttribute:

[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult ChangeAvatar(HttpPostedFileBase file, string Id)
{
    userService.ChangeAvatars(file, Id);
    return RedirectToAction("Index", "Home");
}        

Właśnie wczoraj doszedłem do tego i kod html wygenerował mi się poprawnie lecz nadal Id nie jest przekazywane

0

Chcesz umożliwić userowi zmianę swojego Avatara?

Jeżeli tak, to czy nie lepiej byłoby wyciągać tego Usera z httpcontext w controllerze niż przekazywać Id w formularzu? bo musiałbyś sprawdzać czy faktycznie gość podmienia swój avatar, a nie wstawił tam Id innego usera.

1 użytkowników online, w tym zalogowanych: 0, gości: 1