使用Html.BeginForm中的ActionLink和简单的<a> tag

时间:2017-07-11 13:47:12

标签: c# html asp.net-mvc

In my ASP.NET MVC 5 website I have a menu with two types of files: actual pages returned from the controllers and pdf files that you can view in the navigator. Since my menu is created dynamically (it varies according to Active Directory rights) I have to distinguish which type is which (Document or Page), and this means that according to the file type you click, it will not result in the same action.

To show you, imagine there is a List<DocumentModel> documents and the following to treat the information:

<ul>
@foreach (var document in documents)
{
    <li>
        @if (document.type.Equals("Document"))
        {
            using (Html.BeginForm("DisplayPDF", "Navigation", new { pdfName = document.link }, FormMethod.Post))
            {
                @Html.ActionLink(document.docName, "DisplayPDF", null, new {id="linkStyle", @class = "saveButton", onclick = "return false;" })
            }
        }
        else
        {
         //This link is different then the one above visually
        <a href="~/@document.link" id="linkStyle"><span class="glyphicon glyphicon-file"></span>@document.docName</a>
        }
    </li>
</ul>
<!--This script allows to submit the form, and treat the action of DisplayPDF-->
<script>
    $(document).ready(function () {
        $('.saveButton').click(function () {
            $(this).closest('form')[0].submit();
        });
    });
</script>

DocumentModel is as follows:

public class DocumentModel
{
    public long idDocument { get; set; }
    public long idCategory { get; set; }
    public string docName { get; set; }
    public string link { get; set; }
    public string type { get; set; }
}

Finally my DisplayPDF method is the following:

    public FileContentResult DisplayPDF(string pdfName)
    {
        var fullPathToFile = @"Your\Path\Here" + pdfName;
        var mimeType = "application/pdf";
        var fileContents = System.IO.File.ReadAllBytes(fullPathToFile);
        return new FileContentResult(fileContents, mimeType);
    }

Now as you can see in the Razor view I displayed, there are two types of clickable links. One is only a controller redirection, whereas the second calls the DisplayPDF method and then returns a PDF page. The problem I'm having is that these links are displayed differently, and I don't know how to have the same display for both.

To synthesize my question : how to display the same using @Html.ActionLink() and 'a' tag, knowing that currently, the 'a' tag has the proper display?

Update I modified some code as to give IDs to <a> tag and @Html.ActionLink, then I added this css in stylesheet:

#linkStyle {
    margin-left: 25px;
    color: antiquewhite;
}

But I still have a different display

UPDATE 2 I think that the problem of display come from the BeginForm method, any ideas to fix this?

0 个答案:

没有答案