如何使用asp.net mvc 4和sql server 2008创建Profile用户页面



我想问一下如何使用asp.net mvc 4和sql server 2008创建概要文件用户页面。在我问它之前。我已经在控制器中创建了这样的源代码:

namespace SIDLubricants.Controllers
{
public class ProfileController : Controller
{
    private Tugas_AkhirEntities db = new Tugas_AkhirEntities();
    //
    // GET: /Profile/
    public ActionResult Index(LoginModel model)
    {
        var m_cp_customer = from cp in db.M_cp_Customer.Include(m => m.User) where cp.Id == model.Id select cp;
        return View(m_cp_customer.ToList());

我创建了这样的视图:

<div id="content" class="span12">
<div class="row-fluid sortable">
            <div class="box span12">
                <div class="box-header well">
                    <h2>Contact Person Profile</h2>
                   <table>
                       <% using (Html.BeginForm()) { %>
                       <%: Html.AntiForgeryToken() %>
                       <%: Html.ValidationSummary(true) %>
                       <tr>
                           <td><%: Html.DisplayName("Contact Person Customer") %></td>
                           <td><%: Html.DisplayFor(model => model.kd_cp_customer) %></td>
                       </tr>
                       <tr>
                           <td><%: Html.DisplayName("First Name") %></td>
                           <td><%: Html.DisplayFor(model => model.NamaDepan) %></td>
                       </tr>
                       <tr>
                           <td><%: Html.DisplayName("Last Name") %></td>
                           <td><%: Html.DisplayFor(model => model.NamaBelakang) %></td>
                       </tr>
                       <tr>
                           <td><%: Html.DisplayName("Birth Date") %></td>
                           <td><%: Html.DisplayFor(model => model.Tgl_Lahir) %></td>
                       </tr>
                       <tr>
                           <td><%: Html.DisplayName("Gender") %></td>
                           <td><%: Html.DisplayFor(model => model.JenisKelamin) %></td>
                       </tr>
                       <tr>
                           <td><%: Html.DisplayName("Address") %></td>
                           <td><%: Html.DisplayFor(model => model.Alamat) %></td>
                       </tr>
                       <tr>
                           <td><%: Html.DisplayName("City") %></td>
                           <td><%: Html.DisplayFor(model => model.Kota) %></td>
                       </tr>
                       <tr>
                           <td><%: Html.DisplayName("Province") %></td>
                           <td><%: Html.DisplayFor(model => model.Provinsi) %></td>
                       </tr>
                       <tr>
                           <td><%: Html.DisplayName("Zip Code") %></td>
                           <td><%: Html.DisplayFor(model => model.KodePos) %></td>
                       </tr>
                       <tr>
                           <td><%: Html.DisplayName("Phone") %></td>
                           <td><%: Html.DisplayFor(model => model.Telp) %></td>
                       </tr>
                       <tr>
                           <td><%: Html.DisplayName("Mobile") %></td>
                           <td><%: Html.DisplayFor(model => model.Mobile) %></td>
                       </tr>
                       <tr>
                           <td><%: Html.DisplayName("Email") %></td>
                           <td><%: Html.DisplayFor(model => model.Email) %></td>
                       </tr>
                       <tr>
                           <td><%: Html.DisplayName("UserName") %></td>
                           <td><%: Html.DisplayFor(model => model.User.userName) %></td>
                       </tr>
                       <tr>
                           <td><input type="button" onclick="" value="edit" /></td>
                           <td> <input type="submit" value="Delete" /></td>
                       </tr>
                       <% } %>
                   </table>
                </div>
            </div><!--/span-->
        </div><!--/row-->

但是,我收到了这样的错误消息:

    Server Error in '/' Application.
     --------------------------------------------------------------------------------

 The model item passed into the dictionary is of type 'System.Collections.Generic.List`1[SIDLubricants.Models.M_cp_Customer]', but this dictionary requires a model item of type 'SIDLubricants.Models.M_cp_Customer'

你能帮我吗?我想最后的输出是在操作登录后显示配置文件用户的所有数据。谢谢。。。

问题不在于SQL Server 2008,问题在于返回零个或多个与用户id匹配的实体。您需要将FirstOrDefault()添加到查询中,这样就不会返回IEnumerable<M_cp_Customer>,而是返回单个M_cp_Customer实体。

public ActionResult Index(LoginModel model)
{
    M_cp_Customer m_cp_customer = (from cp in db.M_cp_Customer.Include(m => m.User) 
                                   where cp.Id == model.Id 
                                   select cp).FirstOrDefault();
    return View(m_cp_customer);
}

最新更新