我无所适从,因为我无法将数据加载到视图中。
我们有一个团队正在构建的应用程序,用于处理Sharepoint列表中的数据。
该应用程序是使用MVC.NET Core编写的
该应用程序某种程度上没有使用控制器,而是通过View后端(cshtml.cs)的PostAsync方法执行发布操作
我的任务是在使用共享点列表中的数据加载视图时填充视图
但是,当我尝试在OnGet方法或OnGetAsync方法中分配模型属性时,这些属性是在视图加载之后分配的,并且不会反映在页面上
我对这项技术很陌生
如何将数据正确加载到视图中。我是否应该通过视图后端中的OnGet方法进行操作?
我应该通过控制器来做到吗?如果是,如何将新控制器绑定到现有视图?
无论我尝试了多少,生成的控制器都不会遇到断点
用数据填充加载视图时应该采取什么适当的措施/设计?
在此先感谢您的帮助
这是我到目前为止的代码示例
视图
@page "{ID}"
@using Newtonsoft.Json;
@model ExclusionRequestItemModel
@{
ViewData["Title"] = "Exclusion Request Item Form";
}
@section AdditionalStyles{
<link rel="stylesheet" href="~/css/BIS232.css" asp-append-version="true" />
}
@using (Html.BeginForm("Exclusion Request Item Form", "DefaultController"))
{
<div class="bisformwrapper">
<h2>Exclusion Request Form</h2>
<form method="post">
<div style="width:100%; margin:0 auto;">
<div>
<div asp-validation-summary="All" class="text-danger"></div>
<h3>Product Information</h3>
<span class="glyphicons glyphicons-info-sign"></span>
<div class="jumbotron">
<div class="row form-group">
<div class="col-xs-12 col-md-4">
<div class="bisformdynamiclabel">Product Type</div>
<input class="form-control" type="hidden" asp-for="BIS232Request.Title" />
<input class="form-control" type="hidden" asp-for="BIS232Request.Status" />
<input class="form-control" type="hidden" asp-for="BIS232Request.Product" />
<div id="MetalType">
<div class="bisformdynamiclabel" id="MetalClass">@Html.LabelFor(m => m.BIS232Request.Product)</div>
</div>
</div>
</div>
<br />
</div>
</div>
</form>
</div>
}
视图后端(.cs)
using System;
using System.Collections.Generic;
using System.Linq;
using Newtonsoft.Json.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.ViewFeatures;
using Microsoft.AspNetCore.Mvc.RazorPages;
using BIS232WebProject.Model;
using Newtonsoft.Json;
using System.Net;
using System.Net.Http;
using System.Configuration;
using System.Text;
using Microsoft.AspNetCore.Mvc.Controllers;
using BIS232WebProject.Controllers;
namespace BIS232WebProject.Pages.Forms
{
public class ExclusionRequestItemModel : PageModel
{
public int ItemID { get; set; }
public IActionResult OnGet(int ID)
{
ItemID = ID;
JToken jsonData = null;
Task<string> outerTask = GetExclusionRequestJSON(ID.ToString());
outerTask.ContinueWith(task =>
{
string result = task.Result;
var jobj = JObject.Parse(result);
jsonData = jobj["fields"];
if (jsonData != null)
{
var jobjFields = JObject.Parse(jsonData.ToString());
var JSONData = jobjFields["JSONData"];
if (JSONData != null)
{
var JSONDataFields = JObject.Parse(JSONData.ToString());
BIS232Request.Product = JSONDataFields["Product"].ToString() ?? string.Empty;
BIS232Request.JSONData.MetalClass = JSONDataFields["MetalClass"].ToString() ?? string.Empty;
}
}
});
var requestVm = new ExclusionRequestItemModel();
requestVm.BIS232Request.JSONData.MetalClass = "This was set in the model before rendering the view and should be overwritten.";
return Page();
//return Page(requestVm);
}
//private readonly BIS232Request.JSONData.MetalClas _context;
//public CreateModel(BIS232WebProject.Models.RazorPagesMovieContext context)
//{
// _context = context;
//}
public static async Task<string> GetExclusionRequestJSON(string ID)
{
var status = string.Empty;
try
{
using (var client = new HttpClient())
{
var content = new StringContent("{ID:1}", Encoding.UTF8, "application/json");
//var result = client.GetAsync("https://itasharepointapi.azurewebsites.net/api/GetAllExclusionRequests").Result.Content;
//https://itasharepointapi.azurewebsites.net/api/GetAllExclusionRequests
string requestUrl = "https://itasharepointapi.azurewebsites.net/api/GetExclusionRequestById";
var request = new HttpRequestMessage(new HttpMethod("POST"), requestUrl);
request.Content = new StringContent("{\"ID\":1}", Encoding.UTF8, "application/json");
var response = await client.SendAsync(request);
string jsonContent = response.Content.ReadAsStringAsync().Result;
return jsonContent;
}
}
catch (Exception ex)
{
status = $"Error updating the Sharepoint list: {ex.Message}";
}
return status;
}
[BindProperty]
public ExclusionRequest BIS232Request { get; set; } = new ExclusionRequest()
{
Product = "Steel",
Status = "New Request",
Title = Guid.NewGuid().ToString(),
JSONData = new MetalExclusionData()
{
Ports = new List<string>() { "" },
ProductStandards = new List<ProductStandard>() { new ProductStandard() { Organization = "Please Select", Designation = "" } },
MetalClass = "vasua"
}
//HTSUSCode = "1234"
//,OwnershipActivity = new OwnershipActivity(){
// Ownership =""
//}
};
//public async Task OnGetAsync()
//{
// //Customers = await _db.Customers.AsNoTracking().ToListAsync();
// string vasya = "";
//}
public async Task<IActionResult> OnPostAsync(ExclusionRequest bIS232Request)
{
if (bIS232Request.JSONData.OwnershipActivity == null || bIS232Request.JSONData.OwnershipActivity.Ownership == null)
{
ModelState.AddModelError("OwnershipAnswer", "You must select an option.");
}
if (bIS232Request.JSONData.ExclusionRequesterActivity == null || bIS232Request.JSONData.ExclusionRequesterActivity == "" || bIS232Request.JSONData.ExclusionRequesterActivity == "Please Select")
{
ModelState.AddModelError("ExclusionRequesterActivity", "You must select an option.");
}
if (bIS232Request.JSONData.ExclusionRequesterActivity == "Other" && bIS232Request.JSONData.ExclusionRequesterComments == null)
{
ModelState.AddModelError("ExclusionRequesterActivityComment", "Please provide comments.");
}
if (bIS232Request.JSONData.ExclusionExplanation.Explanation == "Other" && bIS232Request.JSONData.ExclusionExplanation.Comments == null)
{
ModelState.AddModelError("ExclusionExplanationAnswer", "Please provide comments.");
}
//ExclusionExplanationAnswer
if (bIS232Request.JSONData.NonUSProducer == null || bIS232Request.JSONData.NonUSProducer.BehalfOf == null)
{
ModelState.AddModelError("BehalfOfAnswer", "You must select an option.");
}
if (!ModelState.IsValid)
{
return Page();
}
//Do POST to database then redirect to Index
//string bIS232RequestJson = JsonConvert.SerializeObject(bIS232Request);
//string status = await InsertModelIntoSharepointList(bIS232RequestJson);
//set JSONData section from parent object
BIS232Request.JSONData.Status = BIS232Request.Status;
BIS232Request.JSONData.HTSUSCode = BIS232Request.HTSUSCode;
BIS232Request.JSONData.Product = BIS232Request.Product;
return Page();
//return RedirectToPage("../Index");
}
}
}
模型
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Threading.Tasks;
namespace BIS232WebProject.Model
{
public class ExclusionRequest
{
public int ID { get; set; }
[Required]
public string Title { get; set; }
[Required]
public string Product { get; set; }
}
}
答案 0 :(得分:0)
非常感谢您的耐心和理解。我得到了答案,现在可以继续进行了