c# - MVC.Net Controller path with multiple parameters -
[route("add/user/{name}&{state}&{zipcode}&{indeflag}&{email}")] public async task<actionresult> createuser( string name, string state, string zipcode, boolean indeflag, string email) { }
how define route of controller method in example above can pass correct data method?
please help.
you can pass each parameter :
[route("[controller]")] public class usercontroller : controller { // < mvc 6 : [route("add")] [httppost] public async task<iactionresult> create( string name, string state, string zipcode, bool indeflag, string email) { // code here } // if want call in simple query [route("quickadd")] [httpget] public async task<iactionresult> create( string name, string state, string zipcode, bool indeflag, string email) { // code here } // mvc 6 : [route("add")] [httppost] public async task<iactionresult> create( [fromform]string name, [fromform]string state, [fromform]string zipcode, [fromform]bool indeflag, [fromform]string email) { // code here } // if want call in simple query [route("quickadd")] [httpget] public async task<iactionresult> quickadd( [fromquery]string name, [fromquery]string state, [fromquery]string zipcode, [fromquery]bool indeflag, [fromquery]string email) { // code here } }
the url '/user/add' data in form.
other solution, better way, use view model class, :
[route("[controller]")] public class usercontroller : controller { [route("add")] [httppost] public async task<iactionresult> create( addviewmodel model) { if (modelstate.isvalid) { // put code create user here } else { // put code error here } } }
a view model allows use data annotation checks, required fields, or format checks.
using system.componentmodel.dataannotations; public class addviewmodel { [required(errormessage = "please enter name")] [display(name = "username")] [datatype(datatype.text)] public string name { get; set; } public string state{ get; set; } [required] [datatype(datatype.emailaddress)] public string email{ get; set; } [required] [datatype(datatype.phonenumber)] public string phonenumber{ get; set; } public addviewmodel() {} }
Comments
Post a Comment