文本响应显示在视图的未绑定标签中

时间:2014-05-30 13:13:12

标签: c# model-view-controller labels

我有一个包含提交按钮的视图。当单击该提交按钮时,某些代码会运行一个进程。我想将一条文本消息返回到视图上的标签,该标签将让用户知道他们的提交成功或出现错误。

我已经四处寻找并找到了许多关于标签的例子,但是我还没有找到一个能告诉我如何做我想要的事情的例子。

我的控制器:

public ActionResult Import()
{
    //Some code that runs a process
    //Need to know what code will return "Import was Successful" or "Erroring Importing"

    return RedirectToAction("Import")
}

我的观点:

@{
    ViewBag.Title = "Import";
}

<h2>Import</h2>

@using (Html.BeginForm("Importexcel", "Home", FormMethod.Post, new { enctype = "multipart/form-data" }))
{
    <table>
        <tr><td>Import Files</td><td><input type="file" id="FileUpload1" name="FileUpload1" /></td></tr>
        <tr><td></td><td><input type="submit" id="Submit" name="Submit" value="Submit" /></td></tr>
        **<tr><td>@Html.Label(returned results)</td></tr>** // Need to know how to do this

    </table>
}

2 个答案:

答案 0 :(得分:1)

在您看来:

     @using (Html.BeginForm("Importexcel", "Home", FormMethod.Post, new { enctype = "multipart/form-data" }))
{ 

    <table>
    <tr><td>Import Files</td><td><input type="file" id="FileUpload1" name="FileUpload1" /></td></tr>
    <tr><td></td><td><input type="submit" id="Submit" name="Submit" value="Submit" /></td></tr>
    **<tr><td>@Html.Label(returned results)</td></tr>** // Need to know how to do this

</table>  
    @ViewBag.Message
}

在您的控制器中:

    [HttpPost]
    public ActionResult Import(){
    //Some code that runs a process
    //Need to know what code will return "Import was Successful" or "Erroring Importing"

    if(something){
     ViewBag.Message = "Import Failed";
    }
    else
    {
     ViewBag.Message = "Import Successful";
    }
        return View();
    }

尝试拍摄。

答案 1 :(得分:0)

您始终可以通过查询字符串将密钥传递给消息查找表或消息本身。这是一个例子:

控制器操作

public ActionResult Import(string message = null)
{
    // Detect presence of message (i.e. !String.IsNullOrWhiteSpace(message)) and show it.
    // Additional logic after this...

    return RedirectToAction("Import", "YourControllerNameHere", new { message = "Your message here..." });
}

然后,只需在“导入”视图中连接模型或ViewModel,即可显示相应的消息。