Asp.net mvc 3 MVC3 Razor:如何有条件地加载局部视图?

Asp.net mvc 3 MVC3 Razor:如何有条件地加载局部视图?,asp.net-mvc-3,razor,partial-views,Asp.net Mvc 3,Razor,Partial Views,我是MVC3的初学者,还在学习。我尝试编写一个应用程序(MVC3和Razor),允许用户选择文件并上传/保存。在上传/保存过程中,我只想将“等待”文本显示为部分视图。我遇到了问题,因为web应用程序一启动就加载了部分视图,并且我从HomeController-[HTtpPost]等待方法中得到了错误,因为它无法跟踪对象作业中的列表文件。当然,上传后会填写文件列表。我不知道如何解决这个问题,需要你的帮助。先谢谢你 MyHomeController.cs: public ActionResu

我是MVC3的初学者,还在学习。我尝试编写一个应用程序(MVC3和Razor),允许用户选择文件并上传/保存。在上传/保存过程中,我只想将“等待”文本显示为部分视图。我遇到了问题,因为web应用程序一启动就加载了部分视图,并且我从HomeController-[HTtpPost]等待方法中得到了错误,因为它无法跟踪对象作业中的列表文件。当然,上传后会填写文件列表。我不知道如何解决这个问题,需要你的帮助。先谢谢你

MyHomeController.cs

    public ActionResult Index()
    {
        return View();
    }

    [HttpPost]
    public ActionResult UploadFile(IEnumerable<HttpPostedFileBase> attachments)
    {
        foreach ( var file in attachments )
        {
            // do something
        }
        return RedirectToAction("Wait");
    }

    public ActionResult Wait()
    {
            // do something
            ViewBag.Message = "Wait...";
            return View();
    }

    [HttpPost]
    public ActionResult Wait(FormCollection formCollection)
    {
        Work job = MvcApplication.GetWork();
        if ( job.Files.Any() )
        {
            return RedirectToAction("SubmitWork");
        }
        else
        {
            return View();
        }
    }
@{
ViewBag.Title = "FirstTry";
}
<p>
<div id="AddFiles">
    @Html.Partial("_AddFiles")
</div>
</p>

<div id ="Wait">
    @Html.Partial("_Wait")
</div>
@using ( Html.BeginForm("UploadFile", "Home", FormMethod.Post, new{id = "uploadForm", enctype = "multipart/form-data"}) ) 
{
    @(Html.Telerik().Upload().Name("attachments").Multiple(true)
        .Async(async => async.AutoUpload(true) )
    )

    <input type="submit" value="Send" class="t-button" />
    <input type="reset" value="Reset" class="t-button" />
}
public ActionResult Index()
{
返回视图();
}
[HttpPost]
公共操作结果上载文件(IEnumerable附件)
{
foreach(附件中的var文件)
{
//做点什么
}
返回重定向到操作(“等待”);
}
公共操作结果等待()
{
//做点什么
Message=“等待…”;
返回视图();
}
[HttpPost]
公共操作结果等待(FormCollection FormCollection)
{
工作作业=mvcapapplication.GetWork();
if(job.Files.Any())
{
返回重定向到操作(“SubmitWork”);
}
其他的
{
返回视图();
}
}
视图Index.cshtml

    public ActionResult Index()
    {
        return View();
    }

    [HttpPost]
    public ActionResult UploadFile(IEnumerable<HttpPostedFileBase> attachments)
    {
        foreach ( var file in attachments )
        {
            // do something
        }
        return RedirectToAction("Wait");
    }

    public ActionResult Wait()
    {
            // do something
            ViewBag.Message = "Wait...";
            return View();
    }

    [HttpPost]
    public ActionResult Wait(FormCollection formCollection)
    {
        Work job = MvcApplication.GetWork();
        if ( job.Files.Any() )
        {
            return RedirectToAction("SubmitWork");
        }
        else
        {
            return View();
        }
    }
@{
ViewBag.Title = "FirstTry";
}
<p>
<div id="AddFiles">
    @Html.Partial("_AddFiles")
</div>
</p>

<div id ="Wait">
    @Html.Partial("_Wait")
</div>
@using ( Html.BeginForm("UploadFile", "Home", FormMethod.Post, new{id = "uploadForm", enctype = "multipart/form-data"}) ) 
{
    @(Html.Telerik().Upload().Name("attachments").Multiple(true)
        .Async(async => async.AutoUpload(true) )
    )

    <input type="submit" value="Send" class="t-button" />
    <input type="reset" value="Reset" class="t-button" />
}
@{
ViewBag.Title=“FirstTry”;
}

@Html.Partial(“\u AddFiles”)

@Html.Partial(“\u Wait”)
部分视图\u Wait.cshtml

@{
ViewBag.Title = "Wait...";
}
@ViewBag.Message
@using ( Html.BeginForm("Wait", "Home", FormMethod.Post, new
{
    id = "waitform"
}) )
{
}
<script type="text/javascript">
    window.setTimeout("document.getElementById('waitform').submit()", 1000); 
</script>
@{
ViewBag.Title=“等待…”;
}
@查看包。留言
@使用(Html.BeginForm(“Wait”,“Home”),FormMethod.Post,new
{
id=“waitform”
}) )
{
}
setTimeout(“document.getElementById('waitform').submit()”,1000);
部分视图\u AddFiles.cshtml

    public ActionResult Index()
    {
        return View();
    }

    [HttpPost]
    public ActionResult UploadFile(IEnumerable<HttpPostedFileBase> attachments)
    {
        foreach ( var file in attachments )
        {
            // do something
        }
        return RedirectToAction("Wait");
    }

    public ActionResult Wait()
    {
            // do something
            ViewBag.Message = "Wait...";
            return View();
    }

    [HttpPost]
    public ActionResult Wait(FormCollection formCollection)
    {
        Work job = MvcApplication.GetWork();
        if ( job.Files.Any() )
        {
            return RedirectToAction("SubmitWork");
        }
        else
        {
            return View();
        }
    }
@{
ViewBag.Title = "FirstTry";
}
<p>
<div id="AddFiles">
    @Html.Partial("_AddFiles")
</div>
</p>

<div id ="Wait">
    @Html.Partial("_Wait")
</div>
@using ( Html.BeginForm("UploadFile", "Home", FormMethod.Post, new{id = "uploadForm", enctype = "multipart/form-data"}) ) 
{
    @(Html.Telerik().Upload().Name("attachments").Multiple(true)
        .Async(async => async.AutoUpload(true) )
    )

    <input type="submit" value="Send" class="t-button" />
    <input type="reset" value="Reset" class="t-button" />
}
@使用(Html.BeginForm(“UploadFile”,“Home”,FormMethod.Post,new{id=“uploadForm”,enctype=“multipart/form data”}))
{
@(Html.Telerik().Upload().Name(“附件”).Multiple(true)
.Async(Async=>Async.AutoUpload(true))
)
}

MVC不像WebForms那样工作,客户端事件不会传播到服务器控件(实际上甚至没有控件,我认为Telerik模糊了这条线,使MVC体验复杂化了)

您可以在控制器中调用其他操作来下载HTML或JSON或其他内容,但在客户端交换HTML而不更改页面(因为上传正在进行)的唯一方法是使用javascript

我不熟悉这个Telerik控件,但我认为您必须在客户端而不是服务器端执行一些操作,以指示加载进度或显示微调器

他们的API显示有一个
onupload
事件,您可以监听并可能交换到加载div:


他们可能在什么地方有样本。我会看看是否可以挖掘一些东西,但实际上,我认为监听此事件是最好的选择,在客户端而不是服务器端执行此操作。

MVC与WebForms不同,客户端事件不会传播到服务器控件(实际上甚至没有控件,我认为Telerik模糊了这条线,使MVC体验复杂化了)

您可以在控制器中调用其他操作来下载HTML或JSON或其他内容,但在客户端交换HTML而不更改页面(因为上传正在进行)的唯一方法是使用javascript

我不熟悉这个Telerik控件,但我认为您必须在客户端而不是服务器端执行一些操作,以指示加载进度或显示微调器

他们的API显示有一个
onupload
事件,您可以监听并可能交换到加载div:

他们可能在某个地方有一个样本。我会看看我是否能找到一些东西,但我真的认为只听这个事件是最好的选择,在客户端而不是服务器端这样做