开发者

What is the best way to Show list of Model with warning message?

In a controller action, I get some result from a method, this method return a Tuple. The first item in the Tuple, is an IList, the second item is IList.

I'd like return a view to display, the result, the classic way : return View("MyView", TheModel");

but I'd like update a div too to display error or warning message

开发者_运维百科

An idea how do this ? The best way ?

Thanks,


You could use ViewData["Message"] = "Your message"; and then in the View you put this message in your div:

<div>
<%: ViewData["Message"] %>
</div>

Or you could make simple ViewModel have tow properties (which is better than previous):

public class myViewModel{

public string Message {get;set;}
public IList<YourModel> ModelList {get;set;}

}

The Action will look like this:

public ActionResult Index()
{
    var modelList = new List//make your logic to fill YourModel List
    var viewModel = new myViewModel()
                        {
                            Message = "Your Message",
                            ModelList = modelList
                        };
    return View(viewModel);
}

and then make strongly typed view to show your ViewModel

<%@ Control Language="C#" Inherits="System.Web.Mvc.ViewPage<myViewModel>"%>

<div>
<%: Model.Message %>
</div>
    <table>
        <tr>
            <th>
                Model Values
            </th>
        </tr>

    <% foreach (var item in Model.ModelList) { %>

        <tr>
            <td>
                <%: item %>
            </td>
        </tr>

    <% } %>

    </table>

hope this helped ;)


In your controller action:

ModelState.AddModelError("somekey", "some error message");

and in your view:

<div>
    <%: Html.ValidationMessage("somekey") %>
</div>

or include the error message as part of your view model and in your view:

<div>
    <%: Model.ErrorMessage %>
</div>
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜