PDA

View Full Version : ComboBoxFor for foreign key field in FormPanelFor



AntonCharov
Mar 13, 2020, 12:24 PM
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?

fabricio.murta
Mar 13, 2020, 8:13 PM
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
tags.

Here are the threads:
- Tips for creating simplified code samples (http://forums.ext.net/showthread.php?61176-Tips-for-creating-simplified-code-samples)
- More Information Required (http://forums.ext.net/showthread.php?10205-More-Information-Required)
- Forum Guidelines (http://forums.ext.net/showthread.php?3440-Forum-Guidelines-For-Posting-New-Topics)

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 (https://docs.microsoft.com/en-us/ef/ef6/fundamentals/testing/mocking) is what's needed to simplify the world in that test case of yours?

Hope this helps!