0

案例:我的应用程序包含以下模块

  • 客户
  • 项目
  • 业务联系等

客户端模块代码

using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
using System.Linq;
using System.Web;
namespace eCms.Models
{
public class Client
{
    public int ClientId { get; set; }
    public int? AddressId { get; set; }
    public int? ImageFileId { get; set; }

    [StringLength(1000)]
    [Required(ErrorMessage = "Company Name is required")]
    [Display(Name = "Company Name")]
    public string CompanyName { get; set; }

    [Display(Name = "Contact Number")]
    [StringLength(20)]
    public string ContactNumber { get; set; }

    [Display(Name = "Email")]
    [StringLength(100)]
    [DataType(DataType.EmailAddress)]
    [Email]
    public string Email { get; set; }

    [StringLength(260)]
    [DataType(DataType.Url)]
    [Display(Name = "Website")]        public string Website { get; set; }

    [Display(Name = "Show this client on website")]
    public bool IsPublic { get; set; }

    #region << relations >>

    [ForeignKey("AddressId")]
    public virtual Address Address { get; set; }

    [ForeignKey("ImageFileId")]
    public virtual ImageFile ImageFile { get; set; }

    #endregion
}

public class EmailAttribute : RegularExpressionAttribute
{
    public EmailAttribute()
        : base(@"[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}")
    {
        this.ErrorMessage = "Provide a valid email address";
    }
}}

Views/Shared/clients/Index.cshtml 包含以下...

@model IEnumerable<eCms.Models.Client>
@using eCms.Models;
@{
ViewBag.Title = "Index";
eCmsContext db = new eCmsContext();}
<script type="text/javascript" lang="javascript">
function togglesDiv() {
    var catdiv = document.getElementById("addNewCat");
    catdiv.style.display = "none";
    if (catdiv.style.display == "") {
        catdiv.style.display = "none";
    }
    else {
        catdiv.style.display = "";
    }
}
 </script>
 <script type="text/javascript" lang="javascript">
function togglesDivClose() {
    var catdiv = document.getElementById("addNewCat");
        catdiv.style.display = "none";
}</script>
<section class="featured">
<div id="addNewCat" style="display: none; height: 800px; background: #777cdc; padding: 110px">
<div style="float: right">
<a href="#CLIENTS"><span onclick="togglesDivClose();" style="background: #777cdc;">
<img width="30" height="30" src="~/back.png" /></span></a>
</div>
<div style="float: left">
@foreach (var item in db.Clients.ToList()){
<a href="#ClientD"><span onclick="togglesDiv()">
 @* HERe -i want to display projects of selected client in div[named clints] */
</a>}       
</div>
</div>
<a name="ClientD"></a>
</section>
<div id="clients" style="padding: 5px; border: solid 1px #ff6a00; height: 800px; width: 970px; margin: 10px">
@foreach (var item in db.Clients.ToList())
{
<a href="#ClientD"><span onclick="togglesDiv()&GetID()" >
<img src="http://xyz.com/uploads/images/small/@Html.DisplayFor(clientItem => item.ImageFile.FileName)" width="150" height="150" /></span></a>
 }

- togglediv() 是在单击 div 内的图像时创建新的 div[命名为 addNewCat]

客户控制代码...

using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using eCms.Models;
namespace MidasNext.Controllers
{
public class ClientsController : Controller
{
    private eCmsContext db = new eCmsContext();

    //
    // GET: /Clients/

    public ActionResult Index()
    {
        var clients = db.Clients.Include(c => c.Address).Include(c => c.ImageFile);
        return View(clients.ToList());
    }}}

您好 frnds 请帮助 m 通过MVC AJAX解决此问题

问题的详细解释

*v 有一个显示客户列表(图像或徽标)的 div (id=clients)。如果网络用户单击图像,那么它应该创建一个新的 div(id=addNewcat)........(这一切都很好)

但现在 ???在这个新创建的 div(id=addNewcat) 上,我想显示所选客户的项目。

提前谢谢.... :)

4

1 回答 1

0

创建一个 AJAX 方法,该方法转到返回部分视图的控制器方法。在 AJAX 回调函数中,您将 div 的内容设置为结果,类似于:

$.get('/controller/method', { ID: ID },
            function (result) {                
                $('#yourDiv').html(result);
            });
于 2013-02-08T08:34:24.320 回答