Use model property in Url.Content in ASP.Net MVC

SajidHussa
 
on Jul 26, 2021 11:05 PM
Sample_733627.zip
463 Views

use model in url.cntent in mvc

i upload the pdf files is success fully uploaded but now 

<a href="@Url.Content("~/pdffiles/"+model => model.pdfnotes )">Show</a>

not work

i get data in model but how to used no idea

Download FREE API for Word, Excel and PDF in ASP.Net: Download
dharmendr
 
on Jul 27, 2021 05:09 AM

Hi SajidHussa,

Check with below code.

<a href="@Url.Content("~/pdffiles/" + model.pdfnotes)">Show</a>

Check this example. Now please take its reference and correct your code.

Model

public class CustomerModel
{
    public int Id { get; set; }
    public string Name { get; set; }
    public string Country { get; set; }
}

Controller

public class HomeController : Controller
{
    // GET: Home
    public ActionResult Index()
    {
        List<CustomerModel> model = new List<CustomerModel>();
        model.Add(new CustomerModel { Id = 1, Name = "John Hammond", Country = "United States" });
        model.Add(new CustomerModel { Id = 2, Name = "Mudassar Khan", Country = "India" });
        model.Add(new CustomerModel { Id = 3, Name = "Suzanne Mathews", Country = "France" });
        model.Add(new CustomerModel { Id = 4, Name = "Robert Schidner", Country = "Russia" });
        return View(model);
    }

    public ActionResult Details(int id)
    {
        return RedirectToAction("Index");
    }
}

View

@model IEnumerable<Url_Content_Model_MVC.Models.CustomerModel>

@{
    Layout = null;
}

<!DOCTYPE html>

<html>
<head>
    <meta name="viewport" content="width=device-width" />
    <title>Index</title>
</head>
<body>
    <table class="table">
        <tr>
            <th>@Html.DisplayNameFor(model => model.Name)</th>
            <th>@Html.DisplayNameFor(model => model.Country)</th>
            <th></th>
        </tr>

        @foreach (var model in Model)
        {
            <tr>
                <td>@Html.DisplayFor(modelItem => model.Name)</td>
                <td>@Html.DisplayFor(modelItem => model.Country)</td>
                <td><a href="@Url.Content("/Home/Details/" + model.Id)">Show</a></td>
            </tr>
        }

    </table>
</body>
</html>