.Net core Razor Pages add many child rows - one-to-many

How would I handle a model with many children in a razor page Create page?
I have the following models:
public class Invoice
{
public string ClientName;
public DateTime InvoiceDate;
public List<InvoiceItem> InvoiceItems;
}
public class InvoiceItem
{
public string ProductName;
public decimal Qty;
public decimal UnitPrice;
}
I want to add a button in my Invoice Create page that says "Add new item". I will then add (using ajax) a new line on the form with a new InvoiceItem - this I know how to do.
What I don't know is how to set up everything so all the invoice lines will get recognized by the OnPostAsync() method and saved in the database together with the Invoice.
Any pointers or examples would be appreciated.

Related

Application to display food names

I am trying to learn, and I want to create a application that works like this: When they click a button, it will choose a random food(for example baked potato), and whey they click the label(or button) with the food, the recipe for that food will open in a browser.
I have tried making a list or some sort, but not sure how to do this:
<local:People x:Food="ArrayFood">
<sys:String Pasta="One" URL="http://food.com/pasta"/>
<sys:String Corn="Two" URL="http://food.com/corn"/>
<sys:String Salsa="Three" URL="http://food.com/Salsa"/>
</local:People>
I think the problem you're running into is the String type doesn't have those properties of Pasta, Corn, Salsa, or URL.
Probably the easiest thing is not to store that in the resource dictionary at all. I would store a list of custom objects in the ViewModel.
Something like:
public class Food
{
public Food() {}
public Food(string name, string url) {Name=name;Url = url;}
public string Name {get; set;}
public string Url {get; set;}
}
public class MyViewModel
{
public List<Food> Foods {get;set;} = new List<Food>
{
new Food("Corn", "http://..."),
new Food("Pasta", "http://blah/pasta" )
}
.. other view model stuff ..
}
Then bind it to a list view. When they tap, then visit the url

Apache Isis: create dialog with referenced object dropdown

Given an Apache Isis project with a simple domain model such as below, what are the absolute minimum requirements for a create dialogue that will display a dropdown selection of all available Offices when creating a new Person?
Current state: I can create an office objects, list all office objects, but when I want to create a Person, the create dialogue doesn't show a dropdown of the available offices, it just says "(none)" (see screenshot).
Currently my create dialogue for Person looks like this:
How can I display a dropdown of all available offices in this dialogue?
Here is a rough layout of the DOM code I have (JDO annotations omitted):
public class Office {
private String name;
// getter/setter ...
}
public class Person {
private String name;
private Office office;
// getter/setter...
}
and corresponding menu/repository classes such as
public class OfficeRepository {
public List<Office> listAll() {
// ...
}
}
public class PersonRepository {
public Person create(String name, final Office office) {
// ...
}
}
public class PersonMenu {
public static class CreateDomainEvent extends ActionDomainEvent<Person> {}
#Action(domainEvent = CreateDomainEvent.class)
public Person create(
#ParameterLayout(named="Name")
final String name,
final Office office) {
return personRepository.create(name, office);
}
}
You can use the choices supporting method, the autoComplete supporting method, or annotate the referenced class as #DomainObject(bounded=true). The latter is mostly appropriate for reference data objects with a limited (ie bounded) number of instances.
eg:
public List<Office> choices1Create()
return officerRepository.findAll();
}
See these apache isis docs for (links to) further detali.
If you have other questions, I recommend you sign up to the apache.isis mailing list.
Thx
Dan

Get form collection value using mvc paging

How can get form collection value while paging? When i submit form then i can get form collection value. when click next page then i can not get form collection value. so what we need to change in pager?
Kindly suggest me
public ActionResult index(FormCollection fc, int? page)
{
}
Please , page id make a first and must be pass default=0
public ActionResult index( int page, FormCollection fc)
{
}

MVC 4 Validation Attribute is not working for dynamically added fields

Here are my Product and ProductItem classes/models:
public class Product
{
public int ProductId { get; set; }
[Required(ErrorMessage="Enter Name")]
public string Name { get; set; }
public List<ProductItem> productitems { get; set; }
[Required(ErrorMessage="Enter Price")]
public decimal Price { get; set; }
}
public class ProductItem
{
[Required(ErrorMessage="Select Raw Material")]
public int RawMaterial { get; set; }
[Required(ErrorMessage="Enter Quantity")]
public decimal Qty { get; set; }
}
For ProductItem I am adding its fields dynamically with jQuery, as you can see here:
$("#btnAddProductItem").click(function () {
$.getJSON("/rawmaterial/GetRawMaterials", null, function (data) {
var productItem = $("<tr class='productItem' id='productItem-0'><td><select id='rmlist-0' name='productitems[0].RawMaterial'></select><span class='field-validation-valid' data-valmsg-for='productitems[0].RawMaterial' data-valmsg-replace='true'></span></td><td><input type='text' id='rmqty-0' name='productitems[0].Qty'/><span class='field-validation-valid' data-valmsg-for='productitems[0].Qty' data-valmsg-replace='true'></span></td></tr>");
$("#productItem").append(productItem);
$("#rmlist-0").addItems(data);
});
});
Now the validation attributes applied on Name and Price are working fine but not on the fields added dynamically (i.e. "RawMaterial" and "Qty").
Please give me the suggestions how this validation will work ?
Note: For testing purpose I have just added the first object of the List indexed with 0.
There are several ways to accomplish this -
PARTIAL VIEW: Since you are using Server Side data annotation as I see from the class definitions, then it is not a good idea to load dynamically with js. Because you will miss out all the validation that MVC 4 could have created automatically. So, the best solution I would suggest is taking the code that you are adding dynamically to a partial view file and then get the html with ajax call and then populating the HTML.
JS VALIDATION: But, if it is a must that you should use JS, then you have to add all the validation items yourself. To do that you have to do some extra works -
First, inspect the HTML with any developer tools, you will notice that there is a <span> attribute appended after each item to show the error which has a target mentioned. You have to append similar attributes to your elements
With MVC 4 unobtrusive validation, all the validation attributes and rules are added with the target element with data attributes. Each one is based one the validation they stands for. You have you create attributes similar to that.
Finally, after adding all the validation items in JS, reset the form so that it parses the new validations added and work accordingly. The code to parse the validations are here -
var form = $("form") //use more specific selector if you like
form.removeData("validator").removeData("unobtrusiveValidation");
$.validator.unobtrusive.parse(form);
But I would prefer the partial view solution, since it will require least amount of re-work and also gives you option to keep all your validation in one place. You don't have to worry about new validations to be ported to js in future.

Extending a Entity Class with a Partial Class....unsupported type error

I must be doing something really wrong as this seems like a very simple extension that causes an error when you try to compile the code.
So...we have a Customer Table and in that table we have a Customer_ID. We only store the Customer ID and the rest of the data comes from a Customer Truth Center.
This Customer table is referenced and creates a Entity Customer object when we generate our entity EDMX file.
We take this Customer ID and fetch the rest of the Customer info from a WCF service thats our Customer truth center. This returns the Name, Age and such.
So...we want to extend the existing Customer entity with these additional properties however we done "persist" these in our Customer database.
Hence we created a Partial Class to extend our Entity Customer like this:
namespace UsingRIAServices.Web.Models
{
public partial class Customer
{
public string Name { get; set;}
public int Age { get; set;}
}
}
This didnt work and when you build you get the following error.
Entity "UsingRIAServices.Web.Models.Customer' has a property 'CustomerReference' with an supported type.
So...if you go into the Customer.Designer.cs you see this propery
/// <summary>
/// No Metadata Documentation available.
/// </summary>
[BrowsableAttribute(false)]
[DataMemberAttribute()]
public EntityReference<Customer> CustomerReference
{
blah, blah
}
And note the type Customer in the EntityReference which is now an extended class with our partial.
So...I added [Datamember] to each item in our partial class...same error. I tried to [Exclude] it and get the same error.
Why is something that seems so simple and direct so difficult. Please help us figure out how to extend an entity partial class. Can you do this with data that is not in the table?
Thanks
The trick is to add your class to the Models\Shared folder of your web project and name your class Customer.shared.cs.
You then get rid of all of the using statements from your new class and add the "partial" keyword to the new class. For example:
namespace XXXX.Web
{
public partial class Customer
{
public string FullName
{
get
{
return FirstName + " " + LastName;
}
}
}
}