ComboBoxFor for foreign key field in FormPanelFor

  1. #1

    ComboBoxFor for foreign key field in FormPanelFor

    I have two entities - Request and Brigade. Each request belongs to one brigade. I found an example https://mvc.ext.net/#/Models/FormPanelFor/
    Here is my code
    Request.cshtml - It's loads on main page by @Html.partial

    @model GeoSystem.Db.ViewModel.FormPanelRequest
    @(Html.X().FormPanelFor(m => m.request)
    .ID("RequestPanel")
    .X(10).Y(10)
    .BodyPadding(5)
    .DefaultAnchor("100%")
    .Width(300)
    .Buttons(Html.X().Button()
    .Text("Сохранить")
    .DirectClickUrl(Url.Action("SubmitRequest"))
    )
    )
    In Views/Shared/EditorTemplates i add Brigade.cshtml

    @using GeoSystem.Db.ViewModel;
    @model FormPanelRequest

    @(
    Html.X()
    .ComboBoxFor(m => m.request.BrigadeID)
    .Items(FormPanelRequest.getBrigades().Select(d => new ListItem(d.Name, d.BrigadeID)))
    .FieldLabel("Brigade")
    .SimpleSubmit(true)
    .Listeners(l => {
    l.Select.Handler = "this.next().setValue(this.getRawValue());";
    })
    )

    FormPanelrequest.cs

    public class FormPanelRequest
    {
    public Request request { get; set; }
    private static BrigadeRepository repository = new BrigadeRepository();

    public static List<Brigade> getBrigades() {
    return repository.GetAll();
    }
    }

    And i got an exeption

    System.InvalidOperationException: "The context cannot be used while the model is being created.
    This exception may be thrown if the context is used inside the OnModelCreating method or if the same
    context instance is accessed by multiple threads concurrently.
    Note that instance members of DbContext and related classes are not guaranteed to be thread safe

    How to use Entity Framework and Ext.Net?
  2. #2
    Hello @AntonCharov, and welcome to Ext.NET forums!

    Before I get to the thread, please take some time to review our guidelines on posting new threads. Your post is mostly alright despite code we can't run on our side, we just missed your code blocks wrapped between [quote][/quote] tags.

    Here are the threads:
    - Tips for creating simplified code samples
    - More Information Required
    - Forum Guidelines

    Now, to your question, to the best I can infer from what you shared, it seems you may be calling for too early or an async task. You should let the server resolve the result before trying to bind it to components; if you want them to show as part of the page loading process.

    If you need delayed tasks you would need to call separate MVC actions/endpoints in ajax request individually (or coordinately).

    Just by reading your code blocks, for instance, I'd say you'd need the FormPanelResponse bit of data instead of the FormPanelRequest -- but I'm assuming a lot of thing that may not have anything to do with your scenario here. Just that, instead of a "to-be-resolved request" you need the "provided response" in order to populate the components with the <Component>For() approach.

    If you are still unsure how to tread here, I am afraid we would need a runnable (and simplified, please review the threads above, we don't want and are not allowed to have a portion of your proprietary code here, right?) test case. You probably could expand the sample you pointed to use an entity framework query from a statically-defined data (so we don't need database at all). Maybe a mocking framework is what's needed to simplify the world in that test case of yours?

    Hope this helps!
    Fabrício Murta
    Developer & Support Expert

Similar Threads

  1. Grid panel foreign key usage problem
    By AGurel in forum 3.x Help
    Replies: 5
    Last Post: Jan 04, 2016, 1:09 AM
  2. [CLOSED] Window closes on Field ENTER key pressed
    By registrator in forum 2.x Legacy Premium Help
    Replies: 1
    Last Post: Jun 17, 2015, 8:04 AM
  3. Disable Enter key in Text field using MASKRE
    By raju145in in forum 1.x Help
    Replies: 0
    Last Post: Oct 03, 2011, 6:02 AM
  4. Replies: 1
    Last Post: Jul 12, 2011, 5:56 PM
  5. [CLOSED] Problem with Tab Key press in Text Field
    By speedstepmem3 in forum 1.x Legacy Premium Help
    Replies: 3
    Last Post: Sep 24, 2010, 4:24 PM

Tags for this Thread

Posting Permissions