TECHNOLOGIES
FORUMS
JOBS
BOOKS
EVENTS
INTERVIEWS
Live
MORE
LEARN
Training
CAREER
MEMBERS
VIDEOS
NEWS
BLOGS
Sign Up
Login
No unread comment.
View All Comments
No unread message.
View All Messages
No unread notification.
View All Notifications
Answers
Post
An Article
A Blog
A News
A Video
An EBook
An Interview Question
Ask Question
Forums
Monthly Leaders
Forum guidelines
Arifullah Irfann
NA
410
26.8k
How to call asp.net web api from View
Mar 8 2020 5:41 AM
Hello
I have created web api controller
How Can call it for creating and updating?
//////////////To Create
public
IHttpActionResult CreateMovie(MovieDto MovieDto){
if
(!ModelState.IsValid)
return
BadRequest();
var movie = Mapper.Map<MovieDto, Movie>(MovieDto);
db.Movies.Add(movie);
db.SaveChanges();
MovieDto.ID = movie.ID;
return
Created(
new
Uri(Request.RequestUri +
"/"
+ movie.ID), MovieDto);
}
///////To Update
[System.Web.Http.HttpPut]
public
void
UpdateMovie(
int
id, MovieDto MovieDto)
{
if
(!ModelState.IsValid)
throw
new
HttpResponseException(HttpStatusCode.BadRequest); var MovieInDb = db.Movies.SingleOrDefault(m => m.ID == id);
if
(MovieInDb ==
null
)
throw
new
HttpResponseException(HttpStatusCode.NotFound);
Mapper.Map(MovieDto, MovieInDb);
db.SaveChanges();
}
/////////////////Form Code
<form>
<div
class
=
"form-group"
>
@Html.LabelFor(m => m.Name)
@Html.TextBoxFor(m => m.Name,
new
{ @
class
=
"form-control"
})
@Html.ValidationMessageFor(m => m.Name)
</div>
<div
class
=
"form-group"
>
@Html.LabelFor(m => m.DateAdded)
@Html.TextBoxFor(m => m.DateAdded,
"{0:d MMM yyyy}"
,
new
{ @
class
=
"form-control"
})
</div>
<div
class
=
"form-group"
>
@Html.LabelFor(m => m.ReleaseDate)
@Html.TextBoxFor(m => m.ReleaseDate,
"{0:d MMM yyyy}"
,
new
{ @
class
=
"form-control"
})
</div>
<div
class
=
"form-group"
>
@Html.LabelFor(m => m.NumberAvailable)
@Html.TextBoxFor(m => m.NumberAvailable,
new
{ @
class
=
"form-control"
})
@Html.ValidationMessageFor(m => m.NumberAvailable)
</div>
<div
class
=
"form-group"
>
@Html.LabelFor(m => m.GenreID)
@Html.DropDownListFor(m => m.GenreID,
new
SelectList(Model.Genre,
"GenreID"
,
"GenreName"
),
"-Select-"
,
new
{ @
class
=
"form-control"
})
</div>
@Html.HiddenFor(m => m.ID)
@Html.AntiForgeryToken()
<button type=
"submit"
id=
"save"
class
=
"btn btn-primary"
>Save</button> </form>
Thanks.
Attachment:
Files.rar
Reply
Answers (
1
)
how to generate password protected excel using npoi.dll c#
I want to change report title dynamically using local report