简体   繁体   中英

How to integrate two [HttpPost] with the same actionresult name? (MVC)

What i want to do:

Use two httpPost action results, they have the same name (Index) for now.. but when i change the name of either of them and i run the code, nothing happens. how should i rework my code so that all my actionresults run?

       [HttpPost]
    public async Task<ActionResult> Index(HttpPostedFileBase photo)
    {
        ViewBag.hello = "hello world";

        var imageUrl = await imageService.UploadImageAsync(photo);
        ViewBag.Ult = imageUrl;
        //TempData["LatestImage"] = imageUrl;
        return View("Index");



    }

The above code is my first Index actionresult, it runs fine alone, but when i put another one, all hell breaks loose:

    [HttpPost]
    public ActionResult Index(ModelVariables model)
    {  //code
}

Summary: I want to do this:

[HttpPost]
    public async Task<ActionResult> Index(HttpPostedFileBase photo + Modelvariables model)
    {  //code
}

I just want to have one httpPost method, but to include 'photo' and 'model'

You cannot have 2 [HttpPost] methods with the same name. Assuming you have a form with controls for Modelvariables plus a file input, then add both parameters in you POST method

public ActionResult Index(ModelVariables model, HttpPostedFileBase photo)

or better still, add property public HttpPostedFileBase Photo { get; set; } public HttpPostedFileBase Photo { get; set; } public HttpPostedFileBase Photo { get; set; } to you ModelVariables class so that you can use just

public ActionResult Index(ModelVariables model)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM