Managing ASP.NET with the MVC Razor Engine

I am using ASP.NET MVC 3 with the Razor template engine for my site. I currently allow file upload as follows:

<form action="/File/Upload" method="post" enctype="multipart/form-data">
    <label for="file">Upload a file:</label>
    <input type="file" name="file" id="file" /> 
    <input type="submit" name="submit" value="Submit" />
</form>

But I would like to use a third-party control, for example NeatUpload , which allows you to show a progress bar, select multiple files, etc.

In the documentation, they show that the control is used as follows:

<%@ Register TagPrefix="Upload" Namespace="Brettle.Web.NeatUpload"
         Assembly="Brettle.Web.NeatUpload" %>
<Upload:InputFile id="inputFileId" runat="server" />

with some code.

The Razor engine, for obvious reasons, does not like this syntax. Is there any other way to use third-party controls with it, or am I out of luck?

+5
source share
6 answers

, -, MVC. , , Web- โ€‹โ€‹MVC . , , .

. , , .

- Razor .

+4

Razor WebForms. , MVC.

0

, MVCRecaptcha, , , . , , . , , , :

, , RenderControl html :

var captchaControl = new RecaptchaControl { ... }
var htmlWriter = new HtmlTextWriter(new StringWriter());
captchaControl.RenderControl(htmlWriter);
return htmlWriter.InnerWriter.ToString();

, MVC:

class CaptchaValidatorAttribute : ActionFilterAttribute {...}

, :

var recaptchaResponse = captchaValidtor.Validate();

// this will push the result value into a parameter in our Action
filterContext.ActionParameters["captchaValid"] = recaptchaResponse.IsValid;

, :

[CaptchaValidator]
[AcceptVerbs( HttpVerbs.Post )]
public ActionResult CreateComment( Int32 id, bool captchaValid )
{
    if (!captchaValid)
    {
        ModelState.AddModelError("_FORM", "You did not type the verification word correctly. Please try again.");
    }
    // ...
}

, , ASP.Net MVC.

0

You can do this if you just want to visualize the control content and donโ€™t care about registering scripts or postbacks, etc.

Create your own HtmlTextWriter, write the output to it, and then draw this line in your Razor. This is basically an idea from @VeeTheSecond, brought to practice:

@{
    System.Web.UI.WebControls.Label label = new System.Web.UI.WebControls.Label()
    {
        Text = "Hello World!"
    };

    HtmlString renderedControl;

    using (StringWriter w = new StringWriter())
    {
        using (HtmlTextWriter htmlW = new HtmlTextWriter(w))
        {
            label.RenderControl(htmlW);

            renderedControl = new HtmlString(w.ToString());
        }
    }
}
<div>
    @renderedControl
</div>
0
source

Try putting the control in the <form runat = "server"> tag.

-2
source

All Articles