Home > OS >  Model value of an img element is not posting to controller in MVC Razor C#
Model value of an img element is not posting to controller in MVC Razor C#

Time:10-04

**My Controller Code**
//My Controller Action
public ActionResult SelectItemImage(string CustomerLinkItemCode)
        {
            //Creating the Object of model
            ItemImageMaster itemImageMaster = new ItemImageMaster();
            //calling the method to get image byte from database         
            itemImageMaster = 
    CustomerItemLinkRepository.SelectItemImage(CustomerLinkItemCode);         
            //returning the byte as a File
            return File(itemImageMaster.ItemImageData, "image/png");
        }

**My View** 
//calling the controller action from a razor view
<img src="/Kart/SelectItemImage/@Model.CustomerItemLinkMaster.CustomerItemLinkDetailsList[i].CustomerLinkItemCode" alt="" height=75 width=60 id="CustomerLinkItemCode" name="CustomerLinkItemCode"/>

SelectItemImage action is hitting but CustomerLinkItemCode is coming as null.

CodePudding user response:

The default route parameter name in MVC is "id". You can rename your parameter to id:

public ActionResult SelectItemImage(string id)

But you should probably use UrlHelper to begin with. You can use it to generate your URL and if the parameter is not called "id" then the framework should append your key "CustomerLinkItemCode" as a query parameter which your controller should be able to read:

<img src="@Url.Action("SelectItemImage", "Kart", new { CustomerLinkItemCode = <your value> })" />
  • Related