c# - ASP.NET Beginner: Form roundtrip with model validation -
i'm beggining larn asp.net , c# week. until now, i've been developing in php , symfony quite big transition me. i'm learning asp.net book asp.net mvc 4 in action.
i'm trying validate model can't find online help newbie me. model validates contact form name, email , message. simple, right?. not me.
this model...
public class contactmodel { [required(errormessage = "the name has provided, fictional one")] public string name { set; get; } [required(errormessage = "email address has provided, invalid one")] [datatype(datatype.emailaddress)] public string email { set; get; } [required(errormessage = "don't lazy. write message")] [datatype(datatype.multilinetext)] public string message { set; get; } }
i have controller called contactcontroller. has these actions...
public actionresult contact() { contactmodel contactmodel = new contactmodel(); homecoming view(contactmodel); } [httppost] public viewresult savemodel(contactmodel model) { homecoming view(model); }
also, here razor template shows model in ui.
<div class="ui__main main__contact"> <h1 class="ui__main--sectionheader">contact administrators of life</h1> @using (html.beginform("savemodel", "contact", formmethod.post, new { @class="form form__contact" })) { <div class="form__row"> @html.labelfor(model => model.name, new { @class="form__label" }) @html.textboxfor(model => model.name, new { @class="form__texttype form__textinput" }) @html.validationmessagefor(model => model.name) </div> <div class="form__row"> @html.labelfor(model => model.email, new { @class = "form__label" }) @html.textboxfor(model => model.email, new { @class = "form__texttype form__textinput" }) </div> <div class="form__row"> @html.labelfor(model => model.message, new { @class="form__label" }) @html.textarea("message", new { @class = "form__texttype form__textarea" }) </div> <div class="form__row submit__row"> <input id="submit__button" type="submit" value="send" /> </div> }
when user submits form, form have go savemodel
action in contactcontroller
action attribute of <form>
blank action=""
don't think it's going save model
. also, validation messages not displayed. tried @html.validationmessagefor
displays text when don't submit form.
i don't know problem here allow lone find it.
if has suggestions, tutorials, articles or creating form in asp.net mvc 4 , razor, please give me.
i've read article doesn't solve problem. found this stack question , this stack question said, i'm newbie , don't understand problems in questions.
edit:
here routes of app...
routes.maproute( name: null, url: "", defaults: new { controller = "home", action="index" } ); routes.maproute( name: null, url: "contact", defaults: new { controller = "contact", action = "contact" } );
your action
stays empty because mvc framework can not resolve url given controller+action. have explicitly define url in routing table:
routes.maproute( name: null, url: "contact/send-form", defaults: new { controller = "contact", action = "savemodel" } );
or can alternatively generate generic fallback route
routes.maproute( name: null, url: "{controller}/{action}", defaults: new { controller = "home", action = "index" } );
which result in controller/actions available direct urls.
alternatively; can utilize attribute routing routes defined straight @ actions, less looking here & there. have to:
addroutes.mapmvcattributeroutes();
in registerroutes.cs
append attributes actions [route("contact")]
you can re-use same route if http method differs:
[httpget] [route("contact"} public actionresult contact() { contactmodel contactmodel = new contactmodel(); homecoming view(contactmodel); } [httppost] [route("contact")] public viewresult contact(contactmodel model) { if(modelstate.isvalid){ //persist contact form, redirect somewhere } homecoming view(model);//re-render form error messages }
c# asp.net-mvc
No comments:
Post a Comment