Closed net4u closed 4 years ago
I'm comfortable for you to ask anything here as an issue.
It means it's searchable for people that have the same question in the future.
We can just close the issue once the question is answered so it doesn't hang around :)
Feel free to send any shortfire questions to Matt or I via Twitter too.
On 5 Sep 2016, at 4:05 PM, Laurentiu LAZAR notifications@github.com wrote:
Hi,
Where is the best place for asking questions about usage for not perturbing this place with irelevant questions that are not bugs?
Thank you
— You are receiving this because you are subscribed to this thread. Reply to this email directly, view it on GitHub, or mute the thread.
ok, but as I say is not about bugs, but usage scenarios. So first of all: I have an add/registration form as in the picture bellow What I want is to add an extra button just bellow the CUI field and on click to invoke an action/method (I put both in question because I am not sure about approach; at this very moment for testing purpose all is in Index action of relevant controler) that mainly make an web request, return an json object which I use around the relevant model. Is visible on top of view that I render the values that I retrieve from json. But I have no ideea how to draw this extra button and how to invoke on click the needed action/method from controler (and of course not to send yet the form; maybe it will appear more fields that are not available in json, there maybe is a basic information and I may want to manually enter extra details). I stay here for more details is necessary Regards
For being more precise, I have in CustomerControler:
private InvoiceDBEntities db = new InvoiceDBEntities();
//Ask the web for company details
public ActionResult GetCompanyInfo(Customers customer, string CompanyCIF)
{
//call for openapi.ro //pc94 srl: RO5949570 infosystems4u: 22052442
string CompanyCUI = CompanyCIF;
// Create a new 'Uri' object with the specified string.
Uri myUri = new Uri("https://api.openapi.ro/api/companies/" + CompanyCUI + ".json");
// Create a new request to the above mentioned URL.
WebRequest myWebRequest = WebRequest.Create(myUri);
//Add the required header to request
myWebRequest.Headers.Add("x-api-key", "8P4RP_kwn71Nt8VG7boFmQb_7NsihyQxT_x7JGcGQkvPdXZH2Q");
// Assign the response object of 'WebRequest' to a 'WebResponse' variable.
WebResponse myWebResponse = myWebRequest.GetResponse();
// Read the response into a stream
var dataStream = myWebResponse.GetResponseStream();
var reader = new StreamReader(dataStream);
var jsonResultString = reader.ReadToEnd();
// Deserialize
var CompanyInfoData = Newtonsoft.Json.JsonConvert.DeserializeObject<CustomerModels>(jsonResultString);
//Bind to model for feed him
//
var cModel = new Customers();
cModel.Phone1 = CompanyInfoData.telefon;
cModel.CompanyRegistration = CompanyInfoData.numar_reg_com;
cModel.Name = CompanyInfoData.denumire;
cModel.CompanyNumber = CompanyInfoData.cif;
cModel.Address = CompanyInfoData.adresa;
//and others, since the real model may contain more properties than retrieved fields from json
ViewData["Customer"] = cModel;
return View(cModel);
}
and respectively:
// GET: Customers/Create
public ActionResult Create()
{
return View();
}
// POST: Customers/Create
// To protect from overposting attacks, please enable the specific properties you want to bind to, for
// more details see http://go.microsoft.com/fwlink/?LinkId=317598.
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Create([Bind(Include = "CustomerID,Name,CompanyNumber,CompanyRegistration,CompanyBank,CompanyIBAN,Address,CP,City,ContactPerson,Phone1,Phone2,Fax,Email,Notes")] Customers customers)
{
//Here to retrievecompany info from web if available
//Here I want to have the value of field CIF from ChameleonForm as paramter
//GetCompanyInfo(CompanyCIF);
if (ModelState.IsValid)
{
db.Customers.Add(customers);
db.SaveChanges();
return RedirectToAction("Index");
}
return View(customers);
}
So what I want and I don't know yet how to achieve:
1.) To manually inject a HTML button just bellow CIF field as is in print screen
L.E.1
I figured out that by simply writing the button tag (e.g. ) is rendered, anyway is not placed in line with form control but on a new line.
2.) To invoke my method/action on click of button and send the text that I already write in CIF field as parameter to my method/action.
L.E.2
It seems to me that it may be a way as follows
@s.FieldFor(m => m.CompanyNumber) <button type="button" onclick="location.href='@Url.Action("GetCompanyInfo", "Customers", new { CompanyNumber = ViewData["CompanyNumber"] })'" class="btn btn-default">Ask the web!</button>
but still don't send the value of CIF filed as parameter.
3.) If necessary (don't now if necessary, anyway at first experiment I mimic all this in Index action, the CIF value is hardcoded and when the Index view is loaded as seen in print screen the form is prefilled with desired values) to instruct the form to load and render the desired values.
After this I go further with validation and save as normally.
I found a solution for all of above. The only thing that bother me is that I can't render my button just near the edit box. It goes on a new line. I will put the updated code here if you agree, maybe are also others that need it.
Feel free to.
I might be able to suggest ways of achieving what you want.
I haven't had a chance to review what you've posted yet, but will do when I get a chance.
On 6 Sep 2016, at 6:20 PM, Laurentiu LAZAR notifications@github.com wrote:
I found a solution for all of above. The only thing that bother me is that I can't render my button just near the edit box. It goes on a new line. I will put the updated code here if you agree, maybe are also others that need it.
— You are receiving this because you commented. Reply to this email directly, view it on GitHub, or mute the thread.
What I already post to this moment is partially not relevant, since I made changes for made the things working. Now I work to make the code more generic and compact, to be the clear and useful for others. Of course it relies on my scenario where the remote web service returns a JSON to the request, but most do by this way in these days. Probably this evening or tomorrow morning I will add it. Only one thing if can help me with a solution for put the little button in line with the edit. It seems ugly to me being on next line.
OK, now I can provide a sample of what I have: First, code in controller (the concept is about using parameter for retrieving some data from a web service and send it back to the form for usage before form save)
public ActionResult GetCompanyInfo(string CompanyNumber)
{
//call for openapi.ro //for hardcoded test may use e.g. 12345678 as Company dummy ID Code instead of CompanyNumber parameter; it will return 404 error
string CompanyCUI = CompanyNumber;
// Create a new 'Uri' object with the specified string.
Uri myUri = new Uri("https://api.openapi.ro/api/companies/" + CompanyCUI + ".json");
// Create a new request to the above mentioned URL.
WebRequest myWebRequest = WebRequest.Create(myUri);
//Add the required header to request (this is my particular case, as required by api provider
myWebRequest.Headers.Add("x-api-key", "a_personal_key_for_my_api_account");
// Assign the response object of 'WebRequest' to a 'WebResponse' variable.
WebResponse myWebResponse = myWebRequest.GetResponse();
// Read the response into a stream
var dataStream = myWebResponse.GetResponseStream();
var reader = new StreamReader(dataStream);
var jsonResultString = reader.ReadToEnd();
// Deserialize
var CompanyInfoData = Newtonsoft.Json.JsonConvert.DeserializeObject<CustomersJSON>(jsonResultString);
return Json(CompanyInfoData,JsonRequestBehavior.AllowGet);
}
the model where to deserialize is according to expected structure of JSON e.g.
[DisplayName("Ultima prelucrare")]
public string ultima_prelucrare { get; set; }
public string ultima_declaratie { get; set; }
public List<object> tva_la_incasare { get; set; }
public string tva { get; set; }
[DisplayName("Telefon:")]
public string telefon { get; set; }
public string stare { get; set; }
public bool radiata { get; set; }
[DisplayName("Numar RC:")]
public string numar_reg_com { get; set; }
public string judet { get; set; }
public string impozit_profit { get; set; }
public string impozit_micro { get; set; }
public string fax { get; set; }
[DisplayName("Numele companiei:")]
public string denumire { get; set; }
public string cod_postal { get; set; }
[DisplayName("Cod Unic de Inregistrare")]
public string cif { get; set; }
public string adresa { get; set; }
public string act_autorizare { get; set; }
public string accize { get; set; }
and now the view
@using ChameleonForms
@using ChameleonForms.Component
@using ChameleonForms.Enums
@using ChameleonForms.Templates
@using ChameleonForms.Templates.TwitterBootstrap3
@using ChameleonForms.ModelBinders
@model DirectInvoice.Models.Customers
@{ViewBag.Title = "Create";}
<h2>Create Customer</h2>
<p>Use for test e.g. 12345678 (dummy Company Code)</p>
<br />
<div>
@using (var f = Html.BeginChameleonForm())
{
@Html.AntiForgeryToken()
using (var s = f.BeginSection("Add a new customer"))
{
@s.FieldFor(m => m.CompanyNumber)
<button id="Ask" type="button" class="btn btn-default">Ask the web!</button>
@s.FieldFor(m => m.Name)
@s.FieldFor(m => m.CompanyRegistration)
@s.FieldFor(m => m.Phone1).Placeholder("0XX X XXX XXX")
@s.FieldFor(m => m.Fax).Placeholder("0XX X XXX XXX")
@s.FieldFor(m => m.Email).Placeholder("name@email.com")
@s.FieldFor(m => m.Address)
@s.FieldFor(m => m.ZIP)
@s.FieldFor(m => m.City)
}
using (var n = f.BeginNavigation())
{
@n.Submit("Save customer...").WithStyle(EmphasisStyle.Primary)
}
}
</div>
<div>
<hr/>
@Html.ActionLink("Back to List", "Index")
</div>
@section Scripts {
<script type="text/javascript">
$('#Ask').click(function () {
var companynumber = $('#CompanyNumber').val();
$.ajax({
type: 'POST',
data: {
CompanyNumber: companynumber
},
//url for the controller's action method
url: '/Customers/GetCompanyInfo/',
dataType: "json",
success: function (data) {
//Here you would update the textboxes, the 'data' variable contains the html/text from the fill
$('#CompanyNumber').val(data.cif);
$('#CompanyRegistration').val(data.numar_reg_com);
$('#Name').val(data.denumire);
$('#Phone1').val(data.telefon);
$('#Fax').val(data.fax);
$('#Address').val(data.adresa);
$('#ZIP').val(data.cod_postal);
},
error: function () {
//Manage errors
}
});
//console.log();
}
)
</script>
}
Hope that it may be useful for someone.
Hi, I start to use this awesome framework and I'm trying to understand if there is the possibility to create this output in HTML for a select with optgroup.
<select>
<optgroup label="Swedish Cars">
<option value="volvo">Volvo</option>
<option value="saab">Saab</option>
</optgroup>
<optgroup label="German Cars">
<option value="mercedes">Mercedes</option>
<option value="audi">Audi</option>
</optgroup>
</select>
Not in-built, no.
Feel free to submit a PR to add the functionality.
If you want some guidance on where to start let me know.
In the meantime there is nothing stopping you manually defining the HTML for that control and using the OverrideFieldHtml field configuration method to specify it.
On 24 Jan 2017, at 8:55 pm, hanc2006 notifications@github.com wrote:
Hi, I start to use this awesome framework and I'm trying to understand if there is the possibility to create this output in HTML for a select with optgroup.
— You are receiving this because you commented. Reply to this email directly, view it on GitHub, or mute the thread.
In the next days I start to look better at the framework. Since it's a feature that i will use often...I want try to extend it. If you give me some advice where to start I would be grateful.
Thank you
Hi,
Where is the best place for asking questions about usage for not perturbing this place with irelevant questions that are not bugs?
Thank you