0

有2个型号。用户正在登录系统。当他单击论坛视图中的按钮时,我希望将当前模型中的值添加到登录用户的表中。Ogrenci 模型进入系统。单击按钮时,我希望将 ProjectName 添加到 Ogrenci 模型中的 BekleyenProje 列。我怎样才能做到这一点?

型号 1:

public class Ogrenci
{
    public int OgrenciID { get; set; }
    public int OgrenciNumarasi { get; set; }
    public string Ad { get; set; }
    public string Soyad { get; set; }
    public string Bolum { get; set; }
    public short Sinif { get; set; }
    public string Yetenekler { get; set; }
    public string Sifre { get; set; }

    public string BekleyenProje { get; set; }
    public string OnaylananProje { get; set; }

    //FK
    public List<Proje> Projeler { get; set; }
}

型号 2:

public class Proje
{
    public int ProjeID { get; set; }
    public string ProjeAdi { get; set; }
    public string Aciklama { get; set; }
    public DateTime EklenmeTarihi { get; set; }

    //FK
    public int OgrenciID { get; set; }
    public Ogrenci Ogrenci { get; set; }
}

论坛控制器:

public class ForumController : Controller
{
    private OgrenciContext db = new OgrenciContext();

    // GET: Forum
    public ActionResult Index()
    {
        //Include(o => o.Ogrenci) -- öğrenci bilgilerini dahil ediyoruz
        return View(db.Projeler.Include(o => o.Ogrenci).ToList());
    }
}

论坛索引视图(我说的按钮在这里):

@model IEnumerable<DonemProjesi.Models.Proje>

@{
    ViewBag.Title = "Index";
}

<table class="table table-striped table-bordered table-hover table-condensed cols-3 custom_table">
<thead>
    <tr>
        <th scope="col">Proje</th>
        <th scope="col">Etkileşimler</th>
        <th scope="col">Yayınlanma Tarihi</th>
        <th scope="col">Detay</th>
    </tr>
</thead>
<tbody>
    @foreach (var item in Model)
    {
        <tr>
            <td>
                <div>@Html.DisplayFor(modelItem => item.ProjeAdi)</div>
                <small><a href="">@Html.DisplayFor(modelItem => item.Ogrenci.Ad)</a></small>
            </td>
            <td>
                <ul class="activity_outer">
                    <li><strong>03</strong><span>Başvuranlar</span></li>
                    <li><strong>01</strong><span>Dahil olanlar</span></li>
                </ul>
            </td>
            <td>
                <div class="last_activity"><span class="time_ago">@Html.DisplayFor(modelItem => item.EklenmeTarihi)</span></div>
            </td>
            <td>
                <button type="button" class="login-button">@Html.ActionLink("Proje Detayı", "Details", "Proje", new { id = item.ProjeID }, new { @class = "detayy" })</button>
                <button type="button" class="login-button"></button>        //BUTTON IS HERE
            </td>
        </tr>
    }
</tbody>

另外,登录控制器:

public class SecurityController : Controller
{
    OgrenciContext db = new OgrenciContext();
    // GET: Security
    public ActionResult Login()
    {
        return View();
    }

    [HttpPost]
    public ActionResult Login(Ogrenci ogrenci)
    {
        var kullanici = db.Ogrenciler.FirstOrDefault(x=>x.OgrenciNumarasi == ogrenci.OgrenciNumarasi && x.Sifre == ogrenci.Sifre);
        if (kullanici!=null)
        {
            FormsAuthentication.SetAuthCookie(kullanici.Ad, false);
            Session.Add("OgrenciID", kullanici.OgrenciID);      //kimlik doğrulamasu yapılan kullanıcının ID'si alınıyor
            return RedirectToAction("Details","Ogrenci", new {@id=kullanici.OgrenciID });
        }
        else
        {
            ViewBag.Mesaj = "Geçersiz numara veya şifre girdiniz!";
            return View();
        }
    }
}
4

1 回答 1

1

最好使用存储库模式,但直接的解决方案是:

kullanici.BekleyenProje = Request["ProjectName"];
db.SaveChanges();

这也取决于您要传递多少个属性。如果只有一个,可以在Request中发送。否则,您将创建一个包含必要成员的视图模型。

确保按钮正在提交表单,并且 ProjectName 是表单内的隐藏字段。

于 2021-06-08T14:00:57.633 回答