1

これは、画像を表示している宛先ページのデザインです

<img alt="" src="@Url.Action("GetPhoto", "Display")" height="50" width="50" class="photo" />

これは私のソースページで、次のようにバインドlinkbuttonします

@Html.ActionLink("QuestionTitle", "Index", "Display", new { QuestionID = item.QuestionID }, null)

ブラウザで上記のURLをクリックすると、目的地は次のようになります。

http://localhost:1931/Display?QuestionID=1

これは私が画像を表示しようとしている私のコードです

[HttpGet]
public ActionResult GetPhoto()
{
    // int quesID = Convert.ToInt16(Request.QueryString["QuestionID"].ToString()); this didn't worked so by having a look at quick watch I write the below
    int quesID = Convert.ToInt16(Request.UrlReferrer.Query.Substring(12, 1));
        byte[] photo = null;

        var usrname = (from a in db.tblQuestions
                       where a.QuestionID == quesID
                       select new { a.UserName });
        var v = db.tblUsers.Where(p => p.UserName == usrname.FirstOrDefault().UserName).Select(img => img.Photo).FirstOrDefault();
        photo = v;
        return File(photo, "image/jpeg");
    }

querystringこのページを除いて、クエリ文字列を取得できるすべてのページで取得する方法を教えてもらえますか。

4

1 に答える 1

2

宛先ページで使用する必要があります

<img alt="" src="@Url.Action("GetPhoto", "Display", new {QuestionId=Model.Id})" height="50" width="50" class="photo" />

(たとえば、Idプロパティを持つモデルがあると仮定しました)。

これは、ソースページでディスプレイコントローラーのインデックスアクションを呼び出すために使用したのと同じ方法です。

ルートデータなし@Url.Action("GetPhoto", "Display")でリンクを生成しますhttp://localhost:1931/Display/GetPhoto

ルートデータ(3番目の引数)を使用すると、生成されるリンクは次のようになります。http://localhost:1931/Display/GetPhoto?QuestionId=<some value>

于 2013-02-11T13:59:42.473 に答える