数据表型号绑定MVC中 [英] DataTable Model Bind in Mvc

查看:79
本文介绍了数据表型号绑定MVC中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用asp.net的MVC与ASPX视图引擎从一个Excel文件,从提取联系人信息读取,它显示给用户,当他\\她证实将它们保存在数据库中。

现在在我的网页我得到了我的DataTable为模型,并用foreach循环,我表示给用户。然后,我有一个提交按钮,当它在用户点击我希望我的DataTable回控制器中的动作。但我的DataTable为null。

我怎么能有我的DataTable回我的控制器?

ASPX第一行:

 <%@页标题=LANGUAGE =C#的MasterPageFile =〜/查看/共享/ Authenticated.master
继承=System.Web.Mvc.ViewPage&所述; System.Data.DataTable>中%GT;

控制器动作定义:

 公共虚拟的ActionResult SaveExcelContacts(DataTable的DT)


解决方案

我不认为你将能够绑定到数据表上提交,或至少是不很不错的构想。在 DataTable的文档看看 http://msdn.microsoft.com/en-us/library/system.data.datatable.aspx 。这是太复杂,所有的数据发送到客户端并检索它后面的服务器上重建数据表对象。结果
你最好从数据表提取数据到自己的模型类,然后尝试绑定到它提交。不要忘记在您的视图的形式隐藏的输入,因此数据将被张贴到服务器,并能约束。

更新 - 只是增加了一些code片段,可以帮助你解决问题

  //一些数据检索服务
公共接口IUserService {
    ///<总结>
    从数据表中提取///对象MyUserInfo(或excel)
    ///< /总结>
    MyUserInfo GetUserInfo(中间体ID);
    无效SaveUserInfo(MyUserInfo用户信息);
}//模型类
公共类MyUserInfo
{
    公众诠释用户ID {搞定;组; }
    公共字符串名称{;组; }
}//控制器
公共类myController的:控制器
{
    IUserService userService;    [HTTPGET]
    公众的ActionResult的UserInfo(INT用户id)
    {
        VAR用户= userService.GetUserInfo(用户ID);        返回查看(用户);
    }    [HttpPost]
    公众的ActionResult的UserInfo(MyUserInfo myUserInfo)
    {
        userService.SaveUserInfo(myUserInfo);
        返回RedirectToAction(SomeOtherAction,OnControllerOfYou​​rChoice);
    }
}

查看

 <%@页面语言=C#的MasterPageFile =〜/查看/共享/的Site.Master继承=System.Web.Mvc.ViewPage< MyUserInfo>中%GT;
< ASP:内容ID =内容2ContentPlaceHolderID =日程地址搜索Maincontent=服务器>
    &下;使用%(Html.BeginForm(的UserInfo,myController的))
       {%GT;
       <标签>用户名:​​其中; /标签>
       &所述;%= Model.Name%GT;
       &所述;%= Html.HiddenFor(M => m.UserId)%GT;
       &所述;%= Html.HiddenFor(M => m.Name)%GT;
       <输入类型=提交名称=ConfirmUserVALUE =确认/>
    <%}%GT;
< / ASP:内容>

I'm using asp.net mvc with aspx view engine to read from an excel file, extract contact information from that, display it to the user and when he\she confirms it would save them in database.

Now in my page I got my DataTable as Model and with a foreach loop I'm showing that to the user. then I have a submit button that when the user click on it I want my datatable back to the Action in a Controller. but my Datatable is null.

How can I have my datatable back to my controller ?

aspx first line :

<%@ Page Title="" Language="C#" MasterPageFile="~/Views/Shared/Authenticated.master"
Inherits="System.Web.Mvc.ViewPage<System.Data.DataTable>" %>

Controller Action definition :

public virtual ActionResult SaveExcelContacts(DataTable dt)

解决方案

I do not think you will be able to bind to DataTable on submit or at least it is not very good conception. Take a look at the documentation of DataTable http://msdn.microsoft.com/en-us/library/system.data.datatable.aspx. It is too complicated to send all data to client and retrieve it back on the server to reconstruct DataTable object.
You would better extract data from DataTable to your own model class and then try to bind to it on submit. Do not forget to include hidden inputs in the form of your view, so the data will be posted to server and could be bound.

Update - just added some code snippets that could help you solve the problem

// some service for data retrieval
public interface IUserService{
    /// <summary>
    /// Extracts MyUserInfo object from DataTable (or excel)
    /// </summary>
    MyUserInfo GetUserInfo(int id);
    void SaveUserInfo(MyUserInfo userInfo);
}

// model class
public class MyUserInfo
{
    public int UserId { get; set; }
    public string Name { get; set; }
}

//controller
public class MyController : Controller
{
    IUserService userService;

    [HttpGet]
    public ActionResult UserInfo(int userId)
    {
        var user = userService.GetUserInfo(userId);

        return View(user);
    }

    [HttpPost]
    public ActionResult UserInfo(MyUserInfo myUserInfo)
    {
        userService.SaveUserInfo(myUserInfo);
        return RedirectToAction("SomeOtherAction","OnControllerOfYourChoice");
    }
}

View

<%@ Page Language="C#" MasterPageFile="~/Views/Shared/Site.Master" Inherits="System.Web.Mvc.ViewPage<MyUserInfo>" %>
<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">
    <% using (Html.BeginForm("UserInfo","MyController"))
       { %>
       <label>UserName:</label>
       <%= Model.Name %>
       <%= Html.HiddenFor(m => m.UserId) %>
       <%= Html.HiddenFor(m => m.Name) %>
       <input type="submit" name="ConfirmUser" value="Confirm" />
    <%} %>
</asp:Content>

这篇关于数据表型号绑定MVC中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆