简体   繁体   English

ASP.NET MVC DropDownList 通过没有实体框架的存储过程

[英]ASP.NET MVC DropDownList via stored procedure without Entity Framework

I am trying to return a dropdownlist to a partial view (modal button for creating a new document record, need to choose a the document type via dropdown) using a stored procedure.我正在尝试使用存储过程将下拉列表返回到部分视图(用于创建新文档记录的模式按钮,需要通过下拉列表选择文档类型)。

I have tried every way I can find and nothing is working.我已经尝试了所有我能找到的方法,但没有任何效果。 I tried pulling the stored procedure, then putting the results into a SelectListItem and using Html.DropDownListFor , but that caused all kinds of errors.我尝试拉存储过程,然后将结果放入SelectListItem并使用Html.DropDownListFor ,但这会导致各种错误。

Now I am just pulling it into a regular list but getting a null value error on the view.现在我只是将其拉入常规列表,但在视图中出现 null 值错误。 I confirmed that the stored procedure is returning the data - I can get it to populate in a blank view, but once I try to return it in the exact same way to my partial view it breaks.我确认存储过程正在返回数据 - 我可以让它填充在空白视图中,但是一旦我尝试以完全相同的方式将它返回到我的部分视图,它就会中断。

Here is my model:这是我的 model:

public class OrgDocs
{
    public int OrgId { get; set; }
    public int DocId { get; set; }
    public string? DocTypeName { get; set; }
    public string? DocName { get; set; }
    public string? DocPath { get; set; }
    public DateTime CreatedOn { get; set; }

    public IFormFile UploadFile { get; set; }

    public List<OrgDocs> OrgDocList { get; set; }

    public int DocTypeId { get; set; }
}

public class DocTypes
{
    public int DocTypeId { get; set; }
    public string DocTypeName { get; set; }
}

public class DocViews
{
    public OrgDocs OrgDoc { get; set; }
    public DocTypes DocType { get; set; }

    public List<DocTypes> DocTypesList { get; set; }
    public int OrgId { get; set; }
}

Here is my controller:这是我的 controller:

[HttpGet]
// GET: OrgsViewModels/_AddDoc/5
public IActionResult _AddDoc(DocViews docViews)
{
    List<DocTypes> docTypes = PopulateDocTypes();
    docViews.DocTypesList = docTypes;

    return PartialView(docViews);
}
 
[NonAction]
public List<DocTypes> PopulateDocTypes()
{
    using (SqlConnection sqlConnection = new SqlConnection(_configuration.GetConnectionString("SlingshotContext")))
    {
        DataTable dtbl = new DataTable("DocTypes");
        sqlConnection.Open();

        SqlDataAdapter sqlDa = new SqlDataAdapter("DocTypes", sqlConnection);
        sqlDa.SelectCommand.CommandType = System.Data.CommandType.StoredProcedure;
        sqlDa.Fill(dtbl);

        List<DocTypes> docTypes = new List<DocTypes>();
        docTypes = (from DataRow dr in dtbl.Rows
                    select new DocTypes()
                               {
                                   DocTypeId = Convert.ToInt32(dr["DocTypeId"]),
                                   DocTypeName = dr["DocTypeName"].ToString(),
                               }).ToList();

         return docTypes;
    }
}

And here is my view:这是我的观点:

@model Slingshot.Models.DocViews
@{
    ViewData["Title"] = "Add Organization Document";
}

<div class="modal-header">
            <h4 class="modal-title" id="addDocLabel">Add Document</h4>
            <button type="button" class="btn-close" data-bs-dismiss="modal"  aria-label="Close"> </button>
              
        </div>
                     
        <div class="modal-body">
              
  <form enctype="multipart/form-data" asp-action="_AddDoc">
   
        <input type="hidden" asp-for="OrgDoc.OrgId" />
        <div class="form-group">
            <label asp-for="OrgDoc.DocName" class="control-label"></label>
            <input asp-for="OrgDoc.DocName" class="form-control" />
             </div>
     <div class="form-group">
            <select>
            @foreach (var item in Model.DocTypesList)
            {
                <option>
                    @Html.DisplayFor(modelItem => item.DocTypeName)
                </option>
            }

      </select>
 
            
        </div>
         <div class="form-group">
             <input asp-for="OrgDoc.UploadFile" class="custom-file-input" id = "OrgDoc">Choose File</input>
             <label class="custom-file-label" for="OrgDoc" class="form-control"</label>
        </div>
        
        <div class="modal-footer">
            <button type="button" class="btn btn-primary" data-bs-dismiss="modal">Close</button>
            <button type="Submit" value="Submit" class="btn btn-primary">Save</button>

        </div>
        </form>
    </div>

Try using selectlist and dropdownlistfor尝试使用selectlistdropdownlistfor

Step 1 => Create selectlist object in DocView Class

public class DocViews
{
    public OrgDocs OrgDoc { get; set; }
    public DocTypes DocType { get; set; }

    public SelectList DocTypesList { get; set; }
    public int OrgId { get; set; }
}

Step 2 => Inside your _AddDoc controller action method change list to selectlist

[HttpGet]
// GET: OrgsViewModels/_AddDoc/5
public IActionResult _AddDoc(DocViews docViews)
{
    List<DocTypes> docTypes = PopulateDocTypes();
    docViews.DocTypesList = new SelectList(docTypes, "DocTypeId", "DocTypeName");

    return PartialView(docViews);
}

Step 3 => Inside view use dropdownlistfor

<div class="form-group">
    @Html.DropDownListFor(m => m.DocTypesList, Model.DocTypesList as SelectList, new { @class= "form-control", })       
</div>

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用实体框架在ASP.NET MVC中调用存储过程 - How to call stored procedure in ASP.NET MVC with Entity Framework ASP.NET MVC 将参数从 controller 传递到没有实体框架的存储过程 - ASP.NET MVC pass parameter to stored procedure from controller without Entity Framework ASP.Net脚手架MVC努力使用实体框架填充下拉列表 - ASP.Net Scaffolded MVC Struggling to populate a dropdownlist with Entity Framework C#ASP.NET MVC和实体框架6引发错误“找不到存储过程” - C# ASP.NET MVC & Entity Framework 6 throws error “Stored procedure not found” ASP.NET MVC4从带有实体框架的存储过程返回输出 - ASP.NET MVC4 return output from a stored procedure with Entity Framework 我可以使用带有实体框架(asp.net mvc)的另一台服务器上的一台服务器上的存储过程吗? - Can I use a stored procedure from one server on another with entity framework (asp.net mvc)? 如何使用ASP.NET MVC 3实体框架中的DBContext映射存储过程 - How to map a stored procedure using DBContext in ASP.NET MVC 3 Entity Framework 实体框架 ASP.NET MVC 5 中事务中的存储过程和数据库上下文操作 - Stored Procedure and Database Context Operation in a Transaction in Entity Framework ASP.NET MVC 5 使用ASP.NET MVC和Entity Framework将通用列表传递给存储过程 - Pass generic list to stored procedure using ASP.NET MVC and Entity Framework 保存SELECT存储过程ASP.NET实体框架的结果 - Saving the results of a SELECT Stored procedure ASP.NET Entity Framework
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM