English 中文(简体)
• 如何在两个隐蔽领域用raz子作为p.net核心储存选定的价值补贴和选定案文?
原标题:How to store selected value id and selected text on two hidden field using razor asp.net core?

I work on razor asp.net core 6 . 我面临问题,我可以储存某些价值,某些选择案文在隐蔽的田地上消失。

因此,我需要创建两个隐藏的领域。

储存某些价值的首个隐蔽领域

第二版

我曾尝试过:

<div class="col-md-3 col-lg-2">
                        <label for="printerserver-selectlbl" style="margin-left:3px;font-size:15px;font-family:  Open Sans , sans-serif;font-weight: bold;">Print Location</label>

                        <select id="PrinterName-select" asp-for="SelectedPrinterId" name="selectprinterid" class="form-select" style="margin-left:3px;font-size:15px;font-family:  Open Sans  , sans-serif;font-weight: bold;">
                            <option value="0">--Select--</option>
                            @foreach (var printer in Model.PrinterList)
                            {

                                <option value="@printer.UserPC">@printer.PrinterName</option>


                            }
                        </select>

                    </div>

public class LabelPrintingModel : PageModel
    {

        [BindProperty]
        public List<LabelPrinitingView> PrinterList { get; set; }

public async void OnGet()
{
 PrinterList = dtprinters.AsEnumerable()
 .Select(row => new LabelPrinitingView
 {
     // Map the values from the DataRow to the properties of the PrintServer object
     UserPC = (string)row["UserPC"],
     PrinterName = row["PrinterName"].ToString()
     // etc.
 })
 .ToList();
}

 public class LabelPrinitingView
    {
        public string UserPC { get; set; }
        public string PrinterName { get; set; }
    }

我试图做些什么

var selectElement = document.getElementById("PrinterName-select");
        selectElement.addEventListener("change", function () {
            var selectedOption = selectElement.options[selectElement.selectedIndex];
            var selectedText = selectedOption.text;
            var selectedId = selectedOption.id;
            console.log("Selected Text: " + selectedText);
            console.log("Selected ID: " + selectedId);


       <input type="hidden" id="selected-option-text" name="selected-option-text" value="@Model.SelectedOptionText" />
                    <input type="hidden" id="selected-option-value" name="selected-option-value" value="@Model.选用 价值" />

但是,仍然有两种特性在页面模型中无效。

选用 价值

SelectedOptionText

因此,如果是解决问题,请

问题回答
  1. In your PageModel, you need both SelectedOptionValue and SelectedOptionText properties in the page model with the [BindPoperty] attribute in order to get/pass value between the Razor page and page model.
public class LabelPrintingModel : PageModel
{
    [BindProperty]
    public List<LabelPrinitingView> PrinterList { get; set; }

    [BindProperty]
    public string SelectedOptionValue { get; set; }

    [BindProperty]
    public string SelectedOptionText { get; set; }

    ...
}
  1. For the JavaScript part, you are in the correct way which using the event listener to listen to the change event for the <select> element. And don t forget to bind the selected value (id) and text to the <input> element respectively. JS Demo @ StackBlitz
var selectElement = document.getElementById("PrinterName-select");
selectElement.addEventListener("change", function () {
    var selectedOption = selectElement.options[selectElement.selectedIndex];
    var selectedText = selectedOption.text;
    var selectedId = selectedOption.id;
    console.log("Selected Text: " + selectedText);
    console.log("Selected ID: " + selectedId);

    document.getElementById("selected-option-text").value = selectedText;
    document.getElementById("selected-option-value").value = selectedId;
});
  1. For the <input> elements, make sure that you need to set the name attribute the same as the properties you declared in the Page Model.
<input type="hidden" id="selected-option-text" name="SelectedOptionText" value="@Model.SelectedOptionText" />
<input type="hidden" id="selected-option-value" name="SelectedOptionValue" value="@Model.SelectedOptionValue" />

参考:





相关问题
WebForms and ASP.NET MVC co-existence

I am trying to make a WebForms project and ASP.NET MVC per this question. One of the things I ve done to make that happen is that I added a namespaces node to the WebForms web.config: <pages ...

Post back complex object from client side

I m using ASP.NET MVC and Entity Framework. I m going to pass a complex entity to the client side and allow the user to modify it, and post it back to the controller. But I don t know how to do that ...

Create an incremental placeholder in NHaml

What I want to reach is a way to add a script and style placeholder in my master. They will include my initial site.css and jquery.js files. Each haml page or partial can then add their own required ...

asp.net mvc automapper parsing

let s say we have something like this public class Person { public string Name {get; set;} public Country Country {get; set;} } public class PersonViewModel { public Person Person {get; ...

structureMap mocks stub help

I have an BLL that does validation on user input then inserts a parent(PorEO) and then inserts children(PorBoxEO). So there are two calls to the same InsertJCDC. One like this=>InsertJCDC(fakePor)...

ASP.NET MVC: How should it work with subversion?

So, I have an asp.net mvc app that is being worked on by multiple developers in differing capacities. This is our first time working on a mvc app and my first time working with .NET. Our app does not ...

System.Web.Mvc.Controller Initialize

i have the following base controller... public class BaseController : Controller { protected override void Initialize(System.Web.Routing.RequestContext requestContext) { if (...

热门标签