【问题标题】:Asp.net MVC Saving Data from other tables to the Source TableAsp.net MVC 将数据从其他表保存到源表
【发布时间】:2022-10-20 14:29:37
【问题描述】:

我创建了一个表单,它接受用户的输入并将其保存到tblFuelTroubleTickets,例如,我将其他表中的一些字段填充为下拉列表网站将所有站点详细信息与 ClusterOwners 一起存储,这两个表都已连接。

enter image description here

附件是我从用户那里获取一些参数但我不想使用的表格集群所有者名称从用户作为网站存储所有站点详细信息,因此当用户选择站点ID该程序必须从网站并将其保存到tblFuelTroubleTickets以及其他输入细节。

这是视图

@model MyTempWorking.Models.TTCreateModel

@{
     Layout = null;

    if (Session["userID"] == null)
    {
        Response.Redirect("~/Login/Index");
    }
 }

 <!DOCTYPE html>

<html>
<head>
<meta name="viewport" content="width=device-width" />
<title>CreateTT</title>
</head>
<body>
@using (Html.BeginForm()) 
{             
    <div class="form-horizontal">
    <h4>TTCreateModel</h4>
    <hr />
    @Html.ValidationSummary(true, "", new { @class = "text-danger"    })


<div class="form-group">
    @Html.Label("Site ID", htmlAttributes: new { @class = "control-label col-md-2" })
    <div class="col-md-10">

        @Html.DropDownList("SiteCode", ViewBag.sitec as SelectList, "Select Site", new { htmlAttributes = new { @class = "form-control" } })

        @*@Html.DropDownListFor(model => model.SiteCode, ViewBag.sitec as SelectList, "Select Site", new { htmlAttributes = new { @class = "form-control" } })*@
        @*@Html.EditorFor(model => model.SiteCode, new { htmlAttributes = new { @class = "form-control" } })*@
        @*@Html.ValidationMessageFor(model => model.SiteCode, "", new { @class = "text-danger" })*@
    </div>
</div>

<div class="form-group">
        @Html.LabelFor(model => model.RegionCode, htmlAttributes: new { @class = "control-label col-md-2" })
        <div class="col-md-10">
            @Html.EditorFor(model => model.RegionCode, new { htmlAttributes = new { @class = "form-control" } })
            @Html.ValidationMessageFor(model => model.RegionCode, "", new { @class = "text-danger" })
        </div>
 </div>

 <div class="form-group">
        @Html.Label("Area", htmlAttributes: new { @class = "control-label col-md-2" })
        <div class="col-md-10">
            @Html.EditorFor(model => model.AreaCode, new { htmlAttributes = new { @class = "form-control" } })
            @*@Html.EditorFor(model => model.AreaCode, new { htmlAttributes = new { @class = "form-control" } })*@
            @*@Html.ValidationMessageFor(model => model.AreaCode, "", new { @class = "text-danger" })*@
        </div>
 </div>

 <div class="form-group">
        @Html.Label("Visit Type", htmlAttributes: new { @class = "control-label col-md-2" })
        <div class="col-md-10">
            @Html.DropDownListFor(model => model.VisitCode, ViewBag.sitevc as SelectList, "Select Visit Type", new { htmlAttributes = new { @class = "form-control" } })
            @*@Html.EditorFor(model => model.VisitCode, new { htmlAttributes = new { @class = "form-control" } })
    @Html.ValidationMessageFor(model => model.VisitCode, "", new { @class = "text-danger" })*@
        </div>
  </div>


 <div class="form-group">
        @Html.LabelFor(model => model.RequiredFuelFilled, htmlAttributes: new { @class = "control-label col-md-2" })
        <div class="col-md-10">
            @Html.EditorFor(model => model.RequiredFuelFilled, new { htmlAttributes = new { @class = "form-control" } })
            @Html.ValidationMessageFor(model => model.RequiredFuelFilled, "", new { @class = "text-danger" })
        </div>
 </div>

 <div class="form-group">
        @Html.LabelFor(model => model.RequiredVisitDate, htmlAttributes: new { @class = "control-label col-md-2" })
        <div class="col-md-10">
            @Html.EditorFor(model => model.RequiredVisitDate, new { htmlAttributes = new { @class = "form-control" } })
            @Html.ValidationMessageFor(model => model.RequiredVisitDate, "", new { @class = "text-danger" })
        </div>
 </div>

 <div class="form-group">
        @Html.Label("Cluster Owner Name", htmlAttributes: new { @class = "control-label col-md-2" })
        <div class="col-md-10">
            @Html.DropDownListFor(model => model.ClusterOwnerCode, ViewBag.Cuslname as SelectList, "Select Cluster Owner", new { htmlAttributes = new { @class = "form-control" } })
            @*@Html.ValidationMessageFor(model => model.ClusterOwnerCode, "", new { @class = "text-danger" })*@
        </div>
 </div>

 <div class="form-group">
        @Html.Label("CP Status", htmlAttributes: new { @class = "control-label col-md-2" })
        <div class="col-md-10">
            @Html.DropDownListFor(model => model.CPStatusCode, ViewBag.Cpname as SelectList, "Select CP Status", new { htmlAttributes = new { @class = "form-control" } })
            @*@Html.ValidationMessageFor(model => model.CPStatusCode, "", new { @class = "text-danger" })*@
        </div>
 </div>


 <div class="form-group">
        <div class="col-md-offset-2 col-md-10">
            <input type="submit" value="Create" class="btn btn-default" />
        </div>
    </div>
</div>
}

<div>
    @Html.ActionLink("Back to List", "Index")
</div>

这是控制器

[HttpGet]
public ActionResult CreateTT()
{
     List<tblClusterOwner> list = db.tblClusterOwners.ToList();
     ViewBag.Cuslname = new SelectList(list, "ClusterOwnerCode", "ClusterOwnerName");
     List<tblSiteCPStatu> list2 = db.tblSiteCPStatus.ToList();
     ViewBag.Cpname = new SelectList(list2, "CPStatusCode", "CPStatus");
     List<tblSite> list3 = db.tblSites.Where(x => x.Active==true).ToList();
     ViewBag.sitec = new SelectList(list3, "SiteCode", "SiteID");
     List<tblSiteVisitType> list4 = db.tblSiteVisitTypes.ToList();
     ViewBag.sitevc = new SelectList(list4, "VisitCode", "VisitName");
     return View();
 }


[HttpPost]
public ActionResult CreateTT(tblFuelTroubleTicket fctt)
{   
     db.tblFuelTroubleTickets.Add(fctt);            
     fctt.CreatedDate = DateTime.Now;
     fctt.CreatedBy = User.Identity.Name;
     fctt.IsActive = true;
     //fctt.AreaCode=            
     db.SaveChanges();
     return RedirectToAction("DisplayTTs");
 }

我的模特班

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace MyTempWorking.Models
{
    public class TTCreateModel
    {
       public long TT { get; set; }
       public int SiteCode { get; set; }
       public long RegionCode { get; set; }
       public Nullable<long> AreaCode { get; set; }
       public string VisitCode { get; set; }

       [System.ComponentModel.DataAnnotations.DisplayFormat(DataFormatString = "{0:dd/MM/yyyy}")]
       public Nullable<System.DateTime> RequiredVisitDate { get; set; }

       public Nullable<decimal> RequiredFuelFilled { get; set; }
       public string CPStatusCode { get; set; }
       public string SiteStatusCode { get; set; }
       public string InitiatorRemarks { get; set; }
       public Nullable<int> ClusterOwnerCode { get; set; }
       public Nullable<long> VendorCode { get; set; }
       public Nullable<bool> IsActive { get; set; }
       public string CreatedBy { get; set; }
       public Nullable<System.DateTime> CreatedDate { get; set; }
       public string ModifiedBy { get; set; }
       public Nullable<System.DateTime> ModifiedDate { get; set; }
       public string SiteID { get; set; }
       public string VisitName { get; set; }      
       public string AreaName { get; set; }
       public string ClusterOwnerName { get; set; }        
       public string CPStatus { get; set; }
     }
 }

【问题讨论】:

  • 请澄清您的具体问题或提供其他详细信息以准确突出您的需求。正如它目前所写的那样,很难准确地说出你在问什么。

标签: asp.net-mvc entity-framework model-view-controller


【解决方案1】:

所以现在我可以将 excel 放入 HTML 表中。

[1]:https://i.stack.imgur.com/sv09f.png

<table>
  <thead>
       <tr>
        <td>ID</td>
        <td>Product Name</td>
      </tr>
  </thead>

  <tbody>
      <tr>
       <td>1234</td>
       <td>Prod1199</td>
     </tr>
     <tr>
       <td>1235</td>
       <td>Prod1200</td>
     </tr>
and so on......

  </tbody>
</table>

那么我怎样才能将它发送到控制器并检查 ID 在 SQL 表中是否可用,如果可用,然后更新产品名称

【讨论】:

    猜你喜欢
    • 2013-01-08
    • 2013-12-07
    • 1970-01-01
    • 2014-11-19
    • 2012-09-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-07-02
    相关资源
    最近更新 更多